Javascript replace - javascript

Hello struggling here guys..
Is it possible to string replace anything between the the first forward slashes with "" but keep the rest?
e.g. var would be
string "/anything-here-this-needs-to-be-replaced123/but-keep-this";
would end up like this
string "/but-keep-this";
Hope that made sence

You can simply split it by /
var str = "/anything-here-this-needs-to-be-replaced123/but-keep-this";
var myarray = str.split('/');
alert('/' . myarray[2]);

var s = "/anything-here-this-needs-to-be-replaced123/but-keep-this";
pos = s.lastIndexOf("/");
var s2 = s.substring(pos);
alert(s2);

Like this:
var string = "/anything-here-this-needs-to-be-replaced123/but-keep-this";
string = string.substring(string.indexOf('/', 1));
You can view a demo here to play with, the .indexOf() method takes an optional second argument, saying where to start the search, just use that with .substring() here.
If you want to remove all leading slashes (unclear from the example), change it up a bit to .lastIndexOf() without a start argument, like this:
var string = "/anything-here-this-needs-to-be-replaced123/but-keep-this";
string = string.substring(string.lastIndexOf('/'));
You can play with that here, the effect is the same for the example, but would be different in the case of more slashes.

Related

Remove first folder in string

I have a folder path that always starts with a certain string which I want to remove. Let's say it looks like this:
my-bucket/2929023/32822323/file.jpg
I want it to look like this:
2929023/32822323/file.jpg
How would I do that? Thanks!
Using the functions substring and indexOf from String.prototype.
var str = "my-bucket/2929023/32822323/file.jpg";
console.log(str.substring(str.indexOf('/') + 1))
You could use a simple replace method if the string is only present once;
var string = "my-bucket/2929023/32822323/file.jpg";
var revisedString = string.replace('my-bucket/', '');
console.log(revisedString);
However, you're also able to use a Regex (regular expression) to remove it as well, something like;
var string = "my-bucket/2929023/32822323/file.jpg";
console.log(string.replace(/^my-bucket\//, ''));
Use a regex to rip the first one out. No substrings necessary.
var myString= "my-bucket/2929023/32822323/file.jpg";
myString = myString.replace(/^.+?[/]/, '');

Regular Expression to replace part of a string

I need to replace part of a string, it's dynamically generated so I'm never going to know what the string is.
Here's an example "session12_2" I need to replace the 2 at the end with a variable. The "session" text will always be the same but the number will change.
I've tried a standard replace but that didn't work (I didn't think it would).
Here's what I tried:
col1 = col1.replace('_'+oldnumber+'"', '_'+rowparts[2]+'"');
Edit: I'm looking for a reg ex that will replace '_'+oldnumber when it's found as part of a string.
If you will always have the "_" (underscore) as a divider you can do this:
str = str.split("_")[0]+"_"+rowparts[x];
This way you split the string using the underscore and then complete it with what you like, no regex needed.
var re = /session(\d+)_(\d+)/;
var str = 'session12_2';
var subst = 'session$1_'+rowparts[2];
var result = str.replace(re, subst);
Test: https://regex101.com/r/sH8gK8/1

Delete Substring From String Using Regexp in Javascript

I have a string like this
string = '/location/start:2015-06-06/end:2015-06-06'
and I would substitute the start and end paramaters.
Therefore, I would like to delete everything like start:2015-06-06 and end:2015-06-06 by using Regexp.
But I do not know how.
string = string.replace(/start:[\d-]*/g,"").replace(/end:[\d-]*/g,"");
This should work:
var str = '/location/start:2015-06-06/end:2015-06-06';
var result = str.replace(/start:[0-9]{4}-[0-9]{2}-[0-9]{2}/, 'something');

need regex to get string before and after sepearted by a colon

I am having strings like following in javascript
lolo dolo:279bc880-25c6-11e3-bc22-3c970e02b4ec
i want to extract the string before : and after it and store them in a variable. I am not a regex expert so i am not sure how to do this.
No regex needed, use .split()
var x = 'lolo dolo:279bc880-25c6-11e3-bc22-3c970e02b4ec'.split(':');
var before = x[0]
var after = x[1]
Make use of split(),No need of regex.
Delimit your string with :,So it makes your string in to two parts.
var splitter ="lolo dolo:279bc880-25c6-11e3-bc22-3c970e02b4ec".split(':');
var first =splitter[0];
var second =splitter[1];

Extracting elements from a variable

I have a string 'http://this.is.my.url:007/directory1/directory2/index.html' and I need to extract the string as below. Please advice the best way
var one = http://this.is.my.url:007/directory1/directory2/index
Try this:
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
url.replace(/\.[^.]*$/g, ''); // would replace all file extensions at the end.
// or in case you only want to remove .html, do this:
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
url.replace(/\.html$/g, '');
The $ character when included in a regular expression matches to the end of the text string. In variant a you look start at the "." and remove everything from the this character until the end of the string. In variant 2, you reduce this to the exact string ".html". This is more about regular expressions than about javascript. To learn more about it, here is one of many nice tutorials.
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
var trimmedUrl = url.replace('.html', '');
You just need to use replace():
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
var one = url.replace('.html', '');
If want to ensure you only remove the .html from the end of the string use regex:
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
var one = url.replace(/\.html$/', '');
The $ indicates that only the last characters of the string should be checked.
Using a regular expression, it replaces everything (.*) with itself from the capture group (not including the trailing .html).
var url = 'http://this.is.my.url:007/directory1/directory2/index.html';
var one = url.replace(/(.*)\.html/, '$1');
^ ^ ^^
// Capture group ______| |__________||
// Capture ----> Get captured content
You could slice the string up to the last dot:
var url = 'http://this.is.my.url:7/directory1/directory2/index.html';
url = url.slice(0,url.lastIndexOf('.'));
//=> "http://this.is.my.url:7/directory1/directory2/index"
Or in one line:
var url = ''.slice.call(
url='http://this.is.my.url:7/directory1/directory2/index.html',
0,url.lastIndexOf('.') );

Categories