Regex javascript help - javascript

Like usual fighting with myself to understand regex and a I need a help
here is the string:
str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}";
what I need to after regex involved :
Output:
http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1
so everything what is between :
"...{location.href=" ---- and --- ";}"
Thanks for any help !!!

var str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}";
var m = str.match("location\.href='([^']*)");
var url = m[1];

/location\.href='([^']+)'/
The url is contained within the first group.
str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}";
var pattern = /location\.href='([^']+)'/;
if(pattern.test(str)) {
return pattern.exec (str)[1];
}

How about /.*?'([^']*)/?. That's "ignore until the first apostrophe, grab everything that's not an apostrophe".

str = "function onclick(){location.href='http://localhost.com/default.aspx?sectionid=45674356-346-4447-3456-sddwerwertye&languageid=1';}";
str.replace(/.*location.href='(.*)'.*/,"$1");
Would this work in your situation?

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]);

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

how to replace first and last quotes with two different chars in this string "{0}"

I have a string "{0}". I want to replace first quotes with <Q> and second quote with </Q> in javascript.
Can anyone help me with a regex to do this.
I don't have any idea about regex but i thought it seems like this:
var str = "{0}";
var mapObj = {
'{':"<Q>{",
'}':"}</Q>"
};
var re = new RegExp(Object.keys(mapObj).join("|"),"gi");
str = str.replace(re, function(matched){
return mapObj[matched.toLowerCase()];
});
alert(str);
Please correct me, if i'm wrong.

Split string with brace to become parameter

If I have a input value "a[123],b[456],c[789]" and I want to return as "a=123&b=456&c789"
I've tried below code but no luck.. Is there a correct way to implement this?
var str = "a[123],b[456],c[789]"
var string = (str).split(/\[|,|\]/);
alert(string);
One option is:
var rep = { '[': '=', ']': '', ',': '&' };
var query = str.replace(/[[,\]]/g, el => rep[el] );
The delimiters are already there, it's just a matter of replacing one delimiter with another. Replace each [ with an =, replace each , with an &, and remove all ].
var str = "a[123],b[456],c[789]"
var string = str.replace(/([a-z])\[(\d+)],?/g, '$1=$2&').slice(0, -1);
alert(string);
Brute force way im not good at Regex. Just adding my thoughts
var str = "a[123],b[456],c[789]"
str = str.replace(/],/g, '&');
str = str.replace(/\[/g, '=');
str = str.replace(/]/g,'');
alert(str);
The simple 2 line answer for this is:
str=str.replace(/,/g,"&");
str=str.replace(/(\w)\[(\d+)\]/g,"$1=$2");

Remove Quote in Regex Matching

I have the following string:
[assembly: AssemblyVersion("1.0.0.0")]
The issue now is that I have to extract the 1.0.0.0 out. Here's the regular expression that I can come out with:
var pattern = "[^\\/]+\\[[a-z]+:\\s" + "AssemblyVersion"+ "(?:Attribute)?\\((.+)\\)\\]" ;
var theString ="[assembly: AssemblyVersion("1.0.0.0")]";
var reAssemblyVersion = new RegExp(pattern,"m");
reAssemblyVersion.exec(theString);
var theAnswer = RegExp.$1; // theAnswer is "1.0.0.0", but I want it to be 1.0.0.0
There must be something I did wrong in setting up the pattern variable, but couldn't find out.. any ideas?
Your RegEx did not eliminate the double qoutes.
Here is the right one:
var pattern = "[^\\/]+\\[[a-z]+:\\s" + "AssemblyVersion"+ "(?:Attribute)?\\(\\\"(.+)\\\"\\)\\]" ;
// Here --------------------------------------------------------------------^^^^ ^^^^
Hope it helps
Why can't you just have something simple as this:
\(\"([0-9.]*)\"\)

Categories