Javascript replace regex wildcard - javascript

I have a string which I need to run a replace.
string = replace('/blogs/1/2/all-blogs/','');
The values 1, 2 and all-blogs can change. Is it possible to make them wildcards?
Thanks in advance,
Regards

You can use .* as a placeholder for "zero or more of any character here" or .+ for "one or more of any character here". I'm not 100% sure exactly what you're trying to do, but for instance:
var str = "/blogs/1/2/all-blogs/";
str = str.replace(/\/blogs\/.+\/.+\/.+\//, '');
alert(str); // Alerts "", the string is now blank
But if there's more after or before it:
str = "foo/blogs/1/2/all-blogs/bar";
str = str.replace(/\/blogs\/.+\/.+\/.+\//, '');
alert(str); // Alerts "foobar"
Live example
Note that in both of the above, only the first match will be replaced. If you wanted to replace all matches, add a g like this:
str = str.replace(/\/blogs\/.+\/.+\/.+\//g, '');
// ^-- here
You can read up on JavaScript's regular expressions on MDC.

js> 'www.google.de/blogs/1/2/all-blogs'.replace(/\/blogs\/[^\/]+\/[^\/]+\/[^\/]+\/?/, '');
www.google.de

What about just splitting the string at slashes and just replacing the values?
var myURL = '/blogs/1/2/all-blogs/', fragments, newURL;
fragments = myURL.split('/');
fragments[1] = 3;
fragments[2] = 8;
fragments[3] = 'some-specific-blog';
newURL = fragments.join('/');
That should return:
'/blogs/3/8/some-specific-blog'

Try this
(/.+){4}
escape as appropriate

Related

Remove String Between Substring from RegExp

I write this code for remove string from 'a' to 'c'
var str = "abcbbabbabcd";
var re = new RegExp("a.*?c","gi");
str = str.replace(re,"");
console.log(str);
The result in console is "bbd"
But the result that right for me is "bbabbd"
Can I use Regular Expression for this Problem ?
Thank for help.
a(?:(?!a).)*c
Use a lookahead based regex.See demo..*? will consume a as well after first a.To stop it use a lookahead.
https://regex101.com/r/cJ6zQ3/34
EDIT:
a[^a]*c
You can actually use negated character class as you have only 1 character.
The g means it's a global regexp, meaning it picks up both the abc and abbabc in the string. So this does properly remove the items from a..c. It seems you saw only two abc and missed the abbabc. The result bbd is actually correct as it does indeed "remove string from 'a' to 'c'".
abcbbabbabcd => bbd
Here is one more way.
var str = "abcbbabbabcd";
var str= str.replace(/abc/g, "");
console.log(str);
You need to update your regex to a[^ac]*?c , this will avoid character a and c between a and c
var str = "abcbbabbabcd";
var re = new RegExp("a[^ac]*?c","gi");
str = str.replace(re,"");
console.log(str);

Replace multiple occurences of a comment with javascript

I want to replace multiple occurences of comment and try like below
JsFiddle
Code:
var str = '<!--#test--><!--#test1-->'
str = str.replace('<!--/g', '').replace('-->/g', '');
alert(str)
Your problem is that you're trying to use a string instead of a regular expression. For example, this works.
var str = '<!--#test-->'
str = str.replace(/<!--/g, '').replace(/-->/g, '');
alert(str)
Plain regex commands need to be inside //.
Also, use the
Disjunction; Alternative | (pipe character)
str = str.replace(/<!--|-->/g, ''); // #test#test1

Removing spaces, hyphen and brackets from a string

I am getting a string like:
var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';
I want to remove the spaces, hyphen and brackets from every number. Something like:
var str = '+911234567891,432123234,12312313456,4325671234';
Please suggest a way to achieve this.
This will do your job:
var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';
var result = str.replace(/[- )(]/g,'');
alert(result);
You can use Regular Expression to replace those items by empty string:
'+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)'.replace(/[\s()-]+/gi, '');
// results in "+911234567891,432123234,12312313456,4325671234"
Hope it helps.

String replace last character occurrence slash

My url looks like this: ://example/example/.com. I want to remove the last slash of the string. My attempt so far (but doesn't work):
.replace(/\/$/g, '');
Can someone help me along?
You have to escape the slash character in a regular expression literal. Capture the characters after the last slash until the end of the string and use in the replacement:
s = s.replace(/\/([^\/]*)$/, '$1');
(You don't need the g flag for this one, as you know that there is never more than one match.)
Demo: http://jsfiddle.net/Guffa/jkn52/
Alternatively, use a poositive look-ahead to match a slash that doesn't have another slash until the end of the string:
s = s.replace(/\/(?=[^\/]*$)/, '');
Demo: http://jsfiddle.net/Guffa/jkn52/2/
var str = "/1/2/3/4/5"
var index = str.lastIndexOf("/");
var newStr = str.substr(0, index ) + str.substr(index + 1);
console.log(newStr);
demo: http://jsfiddle.net/Jn9bm/
It's a little verbose, but it works:
var url = "//example/example/.com";
var slash_position = url.lastIndexOf('/');
url = url.substr(0, slash_position) + url.substr(slash_position+1);
Try This:
var s= someString.replace(/\//g, "");

Replace a substring with javascript

Need to replace a substring in URL (technically just a string) with javascript.
The string like
http://blah-blah.com/search?par_one=test&par_two=anothertest&SearchableText=TO_REPLACE
or
http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest
means, the word to replace can be either at the most end of the URL or in the middle of it.
I am trying to cover these with the following:
var newWord = NEW_SEARCH_TERM;
var str = 'http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest';
var regex = /^\S+SearchableText=(.*)&?\S*$/;
str = str.replace(regex, newWord);
But no matter what I do I get str = NEW_SEARCH_TERM. Moreover the regular expression when I try it in RegExhibit, selects the word to replace and everything that follows it that is not what I want.
How can I write a universal expression to cover both cases and make the correct string be saved in the variable?
str.replace(/SearchableText=[^&]*/, 'SearchableText=' + newWord)
The \S+ and \S* in your regex match all non-whitespace characters.
You probably want to remove them and the anchors.
http://jsfiddle.net/mplungjan/ZGbsY/
ClyFish did it while I was fiddling
var url1="http://blah-blah.com/search?par_one=test&par_two=anothertest&SearchableText=TO_REPLACE";
var url2 ="http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest"
var newWord = "foo";
function replaceSearch(str,newWord) {
var regex = /SearchableText=[^&]*/;
return str.replace(regex, "SearchableText="+newWord);
}
document.write(replaceSearch(url1,newWord))
document.write('<hr>');
document.write(replaceSearch(url2,newWord))

Categories