Regular expression for URL validation in javascript [duplicate] - javascript

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
I am having form which takes url as input..I want to validate it using javascript and regular expression..
it should accept
www.google.com
google.com
http://google.com
Please suggest me a regular expression and total code if possible for validation of url...
i tried lotta url given on many forums..but couldn't get appropriate answer..
thanks in advance

try this,
^(http\://)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$
this will support
http://google.com
http://www.google.com
www.google.com
google.com

You can test it easily with jQuery :
http://docs.jquery.com/Plugins/Validation/Methods/url.

try dis
url_reg = /(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/;

This one would be fine, (http|https|)(://|)([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
Try it here. works fine - http://www.regular-expressions.info/javascriptexample.html

Related

email regex not working properly with javascript/angular [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 2 years ago.
I have created a regular expression for email validations.
var regex = "^([a-zA-Z]+([\.-_]?[a-zA-z0-9]+)*)\#([a-zA-Z0-9]+)([-][0-9a-z]+)?\.([a-z-]{2,20})(\.[a-z]{2,3})?$"
To match emails:
1. update#update
2. mohit.bhagat#B-9com.com
3. mohit.Bhagat#us.thalesgroup.com
4. mohit#gmail.com.com.com
If you run this over online website 1 and 4th will fail while 2, 3 will pass.
But when I run this code in Javascript( Browser console ), 1st also passes the validation.
I am using Angular application.
The problem is how JS ignores \.
If you do following
var regex = "[a-z]+#[a-z]+\.[a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+.[a-z]{2,3}"
And if you do this
var regex ="[a-z]+#[a-z]+[\.][a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+[.][a-z]{2,3}"
Pay attention to [.] with this now i was able to get validation error for 1st email.
Complete regex: "^([a-zA-Z]+([-_.]?[a-zA-Z0-9])*)#([a-zA-Z0-9]+([-][a-z0-9A-Z]+)?)[.]([a-z]+[-_]?[a-z]+)([.][a-z]{2,3})?$"
Update:
Or you can do this: var regex ="[a-z]+#[a-z]+\\.[a-z]{2,3}"
As mentioned in comments below, you can do this way to consider . by including it in [.], but its a hack and not origional way to do it.
Best way to have . included in your regex is \.

html textarea regex match [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 5 years ago.
I'm developing a chrome and firefox extension and i'm stuck with matching a certain tag and content inside of that. Can you please help me out?
Code:
[QUOTE=UserAdmin;22061013]
[SIZE="4"]
[LEFT]
[COLOR="DarkGreen"] Sample text goes here [/COLOR]
[/LEFT]
[/SIZE]
[/QUOTE]
Here i'd like to match beginning of [QUOTE= because everything what comes after that will be totally different each time and finally by the closing tag of [/QUOTE]
I'm not a regex expert and here is what i've came up with:
const regex = /^(\[QUOTE=)/;
const str = "[QUOTE=UserAdmin;22061013][SIZE="4"] [LEFT] [COLOR="DarkGreen"] Sample text goes here [/COLOR] [/LEFT] [/SIZE][/QUOTE]";
It successfully matched as below but i'm not sure this is the correct way of doing it:
If i can have a regex code to match whatever inside the [QUOTE=]....[/QUOTE] tag and save it to later use would be highly appreciated.
Online regex fiddle link
Try this
\[QUOTE=[\s\S]+\[\/QUOTE\]

Javascript regular expression for URL validation [duplicate]

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 8 years ago.
I want the regular expression to validate the URL with below condition,
It should start with http or https
It should end with the valid domain.
Ex: .com or .in but after the . can have any string. Specially, check whether the . is there and there is a string following the ..
Valid URL: http://www.cnn.com
Invalid URLS:
htt://www.yahoo.com
http://www.yahoo.
http://www.yahoo
I have compose the regular expression as below.
/^(http|https):\/\/[-a-zA-Z0-9+&##/%?=~_|!:,;]+([\-\.]{1}[-a-zA-Z0-9+&##/%?=~_|]+)*\.[a-zA-Z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
It worked fine for most of the scenarios.
But if I enter http://www.yahoo didn't validate correctly, but If I enter http://www.google it throws the validation error.
Can anybody please help me to resolve this issue?
Try this:
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
You can use the following reg exp to match the urls
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
Check it here
https://www.regex101.com/r/jU7iT2/1

How to check string in current URL with javascript? [duplicate]

This question already has answers here:
Get current URL with jQuery?
(33 answers)
Closed 8 years ago.
My Url looks like:
http://mywebsite/series/2545-abc
But I don't know how to check URL with regex. So, How to check url current windows with above URL?
I mean:
If Url = http://mywebsite/series/2545-abc
do something
What about this? The regular expression defined at the first line is tested against the URL:
var myRe = /\/series\//; //regular expression to use
if(myRe.test(window.location.href)){
alert("matching");
}else{
alert("not matching");
}
If you want to test for the whole URL (and really want to use a regular expression for that), you could replace the first line with
var myRe = /^http:\/\/mywebsite\/series\/2545-abc$/;
If you remove the dollar sign at the end, modifications at the end of the URL are accepted as well (e.g. http://mywebsite/series/2545-abc/foo.html)

javascript url regexp restrict multiple http(s)/www [duplicate]

This question already has answers here:
Regular expression for URL
(10 answers)
Closed 8 years ago.
here is my regexp
var url_reg = /^(http[s]?:\/\/|ftp:\/\/)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|in|au)+/;
it works fine for single input like https://www.google.com , but
it allows double or more "http/https/www" like below -
https://www.google.com/https://www.google.com/
url can also include folder like google.com/folder/file
i need to validate single occurrence of valid url.
Can anyone help me?
To validate a URL, you can use a regex. This is what I use. A valid URL per the URL spec. The URL you have provided, is actually a valid URL per the URL spec.
/^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:#&=+$,A-Za-z0-9])+)([).!';/?:,][[:blank:]])?$/
This was borrowed from OSWAP

Categories

Resources