I want to check if string contains a url using javascript i got this code from google
if(new RegExp("[a-zA-Z\d]+://(\w+:\w+@)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(/.*)?").test(status_text)) {
alert("url inside");
}
But this one works only for the url like "http://www.google.com" and "http://google.com" but it doesnt work for "www.google.com" .Also i want to extract that url from string so i can process that url.
Try:
if(new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?").test(status_text)) {
alert("url inside");
}
Sudhir's answer (for me) matches past the end of the url.
Here is my regex to prevent matching past the end of the url.
var str = " some text http://www.loopdeloop.org/index.html aussie bi-monthly animation challenge site."
var urlRE= new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?([^ ])+");
str.match(urlRE)
produced this output using node.js:
[ 'http://www.loopdeloop.org/index.html',
'http://',
undefined,
'www.loopdeloop.org',
undefined,
'l',
index: 11,
input: ' some text http://www.loopdeloop.org/index.html aussie bi-monthly animation challenge site.' ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With