I'm trying to get a regular expression to work where the following URLs are accepted:
www.somesite.com
somesite.com
www.somesite.ca
somesite.ca
somesite.cu.sk.ca
www.somsite.cu.sk.ca
somesite.sk.ca
www.somesite.sk.ca
I have the following so far but it allows www.somesite
^(www\.)?[a-zA-Z0-9_\-]+\.([a-zA-Z]{2,4}|[a-zA-Z]{2}.[a-zA-Z]{2})(.[a-zA-z]{2})?$
Query strings, http, https, ftp are not in play here. Thanks!
You forgot to escape . in the last pattern (.[a-zA-z]{2}) (the dot will match any character):
^(www\.)?[\w-]+\.([a-zA-Z]{2,4}|[a-zA-Z]{2}.[a-zA-Z]{2})(\.[a-zA-z]{2})?$
↑
See DEMO
Also, I replaced your [a-zA-Z0-9_\-] with its equivalent [\w-]
Related
I am validating url on my form through regex.
^(?:http(s)?://)?[\w.-]+(?:.[\w.-]+)+[\w-._~:/?#[]#!\$&'()*+,;=.]+$
It validates all URL for example:
https://www.example.com
http://www.example.com
www.example.com
example.com
http://blog.example.com
http://www.example.com/product
http://www.example.com/products?id=1&page=2
http://www.example.com#up
http://255.255.255.255
255.255.255.255
However it also validates URL like
www.google
www.example
www.example.
www.google.
which are not accepted URL's
I am not too efficient with regex. Please help what needs to be changed
When using a regex in HTML5 pattern attribute you should escape characters very carefully, as those browsers that have ES6+ standard implemented might throw an exception when they "see" [\w\.-] (no need to escape dot, and once the pattern is compiled with u flag, it becomes an error).
Now, to fix the issue, you may add a (?!www\.[^.]+\.?$) lookahead after ^ to fail all inputs that start with www. and then have any 0 or more chars other than . and then an optional . at the end of the string.
You may use
^(?!www\.[^.]+\.?$)(?:https?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w._~:/?#[\\\]#!$&'()*+,;=.-]+$
See the regex demo. Note I escaped both \ and ] in your pattern, I think you meant to match both (your original regex does not match \ with [\w\-\._~:/?#[\]#!\$&'\(\)\*\+,;=.]).
Note that the HTML5 pattern regex is anchored by default, you need no ^ and $ at the start/end:
pattern="(?!www\.[^.]+\.?$)(?:https?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w._~:/?#[\\\]#!$&'()*+,;=.-]+"
But you may still keep them if you want.
To allow only emails with TLD (ending with .de or .com) I want to use the following pattern:
^[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-zA-Z]{2,4}$
I tested this regular expression on regexr.com a couple of times and it worked good, for example it did not match with test#test.
But the Angular Validator says no error for test#test with this pattern Validator:
Validators.pattern('^[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-zA-Z]{2,4}$')
How is that possible?
You have to escape the backslash, since it's a string.
'^[a-z0-9._%+-]+#[a-z0-9.-]+\\.[a-zA-Z]{2,4}$'
I am trying to write a regular expression in which I want to compare the URL's.
Any URL Matches
http://*.xyz.com
Except or Excluding
http://m.xyz.com and http://m.product.xyz.com
So far I was trying to do it by using if else in RegExp but I couldn't be able to do it right...
(^http:\/\/)(((1)<!(m|m\.product))\.xyz\.co\.jp)?
You can try that:
^http:\/\/(?!m\.xyz\.com|m\.product\.xyz\.com).*\.xyz\.com$
Regex101 Demo
https?:\/\/(?!m\.|m\.product\.).*\.xyz\..*
This regex accepts all *.xyz.* domains except m.xyz.* and m.product.xyz.*. Also takes care of http or https.
Demo
I'm struggling with forming a regex that would match:
Just domain in case of URL
Whole string in case of no URL
Acceptance test (regex should match bold text):
http://mozart.co.uk
https://avocado.si/hmm
http://www.qwe123qwe.com
Starbucks
Benchmark 123
So far I've come up with this:
([^\/\/]+)(?:,|$)
It works fine, but not for URLs with trailing slash on the end. How can I modify the expression to include full path (everything on the right side of http(s)://) as well? Thank you.
This regex will match them if it starts with http:// or https:// until the next slash. If it doesn't start with http:// nor https:// then it will match the whole string. Close enough?
(?:^https?:\/\/([^\/]+)(?:[\/,]|$)|^(.*)$)
I should note that most languages have functions built in to properly parse URLs and these are preferable.
You should note that I've got 2 sets of capturing parentheses, so depending on your language that may be significant.
Maybe that ^(http[s]?:\/\/)?(.*)$. Play here: https://regex101.com/r/iZ2vL4/1
This will have Matching groups, the domain you want will be in the 4th matching group.
/^((http[s]?|ftp):\/\/)?\/?([^\/\.]+\.)*?([^\/\.]+\.[^:\/\s\.]{1,3}(\.[^:\/\s\.]{1,2})?(:\d+)?)($|\/)([^#?\s]+)?(.*?)?(#[\w\-]+)?$/mg
Regex101.com workbench to check out your URLs just paste them in the "TEST STRING" Textbox to test it out.
Don't recall where I got this... so I don't know who to credit. But it's pretty slick!
All,
I am new to REGEX world...
I know that there are lot of regex avail for validating the common URL with http in it.
But I am looking for a regex to validate the URL in the following formats(without HTTP/HTTPS):
www.example.com/user/login
www.example.com
www.exmaple.co.xx
www.example.com/user?id=234&name=fname
in case if the URL contains only,
www.example(without the domain - .com OR .co.xx)
example.com (without "www")
I should throw an error to the user.
any help would be highly appreciated...
Thanks
Raj
This regex will pass your first set, but not match the second set:
^www\.example\.(com|co.xx)(/.*)?$
In English, this regex requires:
starts with www.example.
followed by either com or co.xx
optionally followed by / then anything
You could be more prescriptive about what can follow the optional slash by replacing (/.*) with (/(user|buy|sell)\?.*) etc