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

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

Related

PHP : Regex to extract data from web url [duplicate]

This question already has answers here:
Parsing domain from a URL
(19 answers)
Closed 5 years ago.
I am receiving URL from an application as post data.
The URL has a signature : //abc.com/name?q=123456.
I want to extract name from the URL using regex ( Regular Expression )
How do I do that?
Best method is parsing url, but if you need regex to extract name :
/.+\/([^\?]+?)(\?|$)/gm
Demo
Note: this regex pattern match whole url, but captures name that you want in Group 1.

Complicated JavaScript string removal/replacement [duplicate]

This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 7 years ago.
I have a query string that I need to remove a certain parameter from. For instance, my query string may be "?name=John&page=12&mfgid=320", and I need to remove the "page" parameter from it and end up with "?name=John&mfgid=320". I cannot assume that the "page" parameter is or isn't followed by other parameters.
All my attempts at using JavaScript functions/regex are failing miserably, so I could really use a hand in getting this working. Thanks.
That's quite easy... It's just /page=\d+&?/
var uri = '?name=John&page=12&mfgid=320';
uri = uri.replace(/page=\d+&?/,'');
You can use:
uri = uri.replace(/[?&]page=[^&\n]+$|([&?])page=[^&\n]+&/g, '$1');
RegEx Demo
We'll need to use alternation to cover all the cases of presence of query parameter. Check my demo for all test cases.

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

JavaScript regex for URL [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 have to run a JavaScript regex on URLs to detect any (one or more) special character after URL and put space before them, like:
url, --> url , or url), --> url ),
e.g.
(https://www.microsoft.com), into (https://www.microsoft.com ),
var myregexp =/(\b(https?|ftp|file|http):\/\/[\-A-Za-z0-9+&##\/%?=~_|!:,.;]*[\-A-Za-z0-9+&##\/%=~_|])/g;
var str="http://www.microsoft.com)"; //url
var res=myregexp.exec(str); //execute regex query
str=str.substring(0,myregexp.lastIndex)+' '+str.substring(myregexp.lastIndex);
I used Regex query for url from : https://stackoverflow.com/a/163684

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)

Categories

Resources