Replace multiple occurences of a comment with javascript - 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

Related

Regex remove all string start with special character

I have a string look like:
var str = https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none
I want to remove at start ?pid= to end. The result look like:
var str = https://sharengay.com/movie13.m3u8
I tried to:
str = str.replace(/^(?:?pid=)+/g, "");
But it show error like:
Invalid regular expression: /^(?:?pid=)+/: Nothing to repeat
If you really want to do this at the string level with regex, it's simply replacing /\?pid=.*$/ with "":
str = str.replace(/\?pid=.*$/, "");
That matches ?pid= and everything that follows it (.*) through the end of the string ($).
Live Example:
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.replace(/\?pid=.*$/, "");
console.log(str);
You can use split
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none"
var result = str.split("?pid=")[0];
console.log(result);
You can simply use split(), which i think is simple and easy.
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.split("?pid");
console.log(str[0]);
You may create a URL object and concatenate the origin and the pathname:
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
var url = new URL(str);
console.log(url.origin + url.pathname);
You have to escape the ? and if you want to remove everything from that point you also need a .+:
str = str.replace(/\?pid=.+$/, "")
You can use split function to get only url without query string.
Here is the example.
var str = 'https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none';
var data = str.split("?");
alert(data[0]);

How to replace two characters at the same time with js?

Below is my code.
var str = 'test//123_456';
var new_str = str .replace(/\//g, '').replace(/_/g, '');
console.log(new_str);
It will print test123456 on the screen.
My question is how to do it in same regular express? not replace string twice.
Thanks.
Use character class in the regex to match any character in the collection. Although use repetition (+, 1 or more) for replacing // in a single match.
var new_str = str .replace(/[/_]+/g, '');
var str = 'test//123_456';
var new_str = str.replace(/[/_]+/g, '');
console.log(new_str);
FYI : Inside the character class, there is no need to escape the forward slash(in case of Javascript RegExp).
Use the regex to match the list of character by using regex character class.
var str = "test//123_456";
var nstr = str.replace(/[\/_]/g, '');

Regex capture character set but not some specific characters

Javascript regex
I want to capture all characters in this set
([-A-Z0-9+&##\/%=~._|\?]{2,})
but I dont want ending ~~ || ## to be captured.
Eg:
It#was#022342#whate#~f56#|fdsdfw&~~
should result in caputre of
It#was#022342#whate#~f56#|fdsdfw&
There you go:
re = /([-A-Z0-9+&##\/%=~._|\?]{2,}?)(?:(~~|##|\|\|)+)/i
Working example:
re = /([-A-Z0-9+&##\/%=~._|\?]{2,}?)(?:(~~|##|\|\|)+)/i
str = "It#was#022342#whate#~f56#|fdsdfw&~~"
console.log(str.match(re)[1])
str = "It#was#022342#whate#~f56#|fdsdfw&##"
console.log(str.match(re)[1])
str = "It#was#022342#whate#~f56#|fdsdfw&||"
console.log(str.match(re)[1])
str = "It#was#022342#whate#~f56#|fdsdfw&||##"
console.log(str.match(re)[1])
str = "It#was#022342#whate#~f56#|fdsdfw&##||"
console.log(str.match(re)[1])
There's probably a cleverer way to do this, but here's a pretty straightforward approach:
([-A-Za-z0-9+&##\/%=~._|\?]{2,}?)(?:~~|\|\||##)?$
https://regex101.com/r/E9htiV/1
Is this what you are looking for?
str.replace(/[\|#~]*$/,'');
For example:
var str = "It#was#022342#whate#~f56#|fdsdfw&~~";
str.replace(/[\|#~]*$/,'');
"It#was#022342#whate#~f56#|fdsdfw&"
Also works if you have any combination of ~, # and | at the end
var str = "It#was#022342#whate#~f56#|fdsdfw&~#|"
str.replace(/[\|#~]*$/,'');
"It#was#022342#whate#~f56#|fdsdfw&"
As of the comments above, I think you want to remove ~~ || ## from your string str, you can do it simply like this:
str.replace(/(~~|\|\||##)/g, "");
Or if you want to remove just from the end of your string
str.replace(/(~~|\|\||##)+$/, "");

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.

Javascript replace regex wildcard

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

Categories