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(/(~~|\|\||##)+$/, "");
Related
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]);
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, '');
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);
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
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