I am trying to write a regex which will do the following
If I have url like
example.com?mask=33&filter=23423&mode=12
It will match substring filter=23423
I need also to consider that the url can be
example.com?filter=23423
Thanks in advance.
You may just not consider [\?&] in your regexp and put /filter=[0-9]*/ if you sure that there will not be any params like other_filter=987 in your url.
If you may have such use substr on the resulted regexp like so
url.match(/[\?&]filter=[0-9]*/)[0].substr(1)
Related
I have a URL string like this:
https://abc.abc.com/appp.WApi/api/v1/endpoint/picklist/?password=mypassword&username=myuser
I want to filter out the password and replace it:
https://abc.abc.com/appp.WApi/api/v1/endpoint/picklist/?password=PLACEHOLDER&username=myuser
How can I do this with regular expressions?
I tried to do this:
replace(/password=*&/, 'PLACEHOLDER');
and this:
replace(/password=(.+)&/, 'PLACEHOLDER');
but it did not work
The regex for this would be password=(.+?)& this assumes you have URL encoded the password and a simple js replace on the URL should fix it...
However, if you're sending passwords in a get request I think you might have bigger problems...
This is the regular expression that works:
url.replace(/password=(.*?)&/g, 'password=PLACEHOLDER&')
I have the following: "/assets/blt49012d3c84501502/CA_the_first_avenger.jpg"... "/assets/blt6d6c300f37d5df8c/husky_puppies.jpg" inside a huge string text (json data stringified)
I am not sure how to write a regex for it, I've tried doing: /\/assets\/blt[a-zA-Z0-9]+/g but it only returns the first part : /assets/blt49012d3c84501502
thanks!
I think this is the regex you are looking for:
/(\/assets\/\w*?\/\w*?\.jpg)/g
You can test it out here
This should do the trick --
\/assets\/blt[a-zA-Z0-9]+\/[\D|_]+\.jpg
https://regex101.com/r/aP4gI0/1
I'm using JavaScript regular expression for web url validation. I've a lot of regular expression but didn't find which could fulfill my requirements.
var myRegExp =/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i;
I also remove the http or https before my url its not necessary
var myRegExp =/^(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i;
this is what last I tried but didn't succeed I want regular expression as per my requirement like
www.google.com
www.google.us
www.google.com.us
www.google.net
www.google.org
www.google.co
and I also tried here and other links also
Try this regex.
Works for me..
/(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/
^(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$
Try this.See demo.
https://regex101.com/r/dL7oF8/1
there are a lot more TLDs than that. Why not loosen your regex to something like this?
var myRegExp = /^(\w+\.)+(\w+)$/;
I'm trying to write a regexp which allow me to filter out all the url addresses having this shape.
http://foo.com/
http://foo
http://foo.*
But not the following:
http://foo.com/subfoo
http://foo.com/subfoo/
http://foo.com/subfoo/subsubfoo..
In order to match the second url group i've written the following regexp:
http://.*/.
However my problem is search the regexp matching the first group.
So i need a way to say:
if after http://.* or http.//.*/ there is nothing, matches the pattern.
I've read something on lookhaead. I don't know it this might be the right way.
Any idea? Thanks for your answer.
A bit late, but this worked for me:
http://[^/]*[/]*$
/^http:\/\/[^/?#]*\/./i
should match only http URLs with a path component other than /.
Don't forget query strings like this: http://foo.com/?search
/^http:\/\/[^/?#]*\/[^?]/i
I am new to JavaScript
i need a regular expression which
allows both of this forms
for example :
http://www.google.com
www.google.com
var url_pattern = new RegExp("((ftp|http|https)(:\/\/))?([a-zA-Z0-9]+[.]{1}){2}[a-zA-z0-9]+(\/{1}[a-zA-Z0-9]+)*\/?", "i");
return url_pattern.test(url);
(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?
This works quite well.
I've just written up a blog post on recognising URLs in most used formats such as:
www.google.com
http://www.google.com
mailto:somebody#google.com
somebody#google.com
www.url-with-querystring.com/?url=has-querystring
The regular expression used is /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+#)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%#.\w_]*)#?(?:[\w]*))?)/ however I would recommend you got to http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without-the to see a complete working example along with an explanation of the regular expression in case you need to extend or tweak it.
^((?:(?:https?|ftp):)?\/\/?)?(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$
Test it! https://regex101.com/r/qQ8uV6/1
Extracted from In search of the perfect URL validation regex https://mathiasbynens.be/demo/url-regex (modified it a bit).
credits to #diegoperini.
If you test it in JavaScript, you'll get a nice
ParseError: Error parsing regular expression: Invalid regular expression
....
Range out of order in character class
You need to replace \x{xxxx} for \uxxxx. So in JavaScript it'll be:
^(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\ufff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$
Just like stated in JavaScript Unicode Regex - Range out of order in character class