10

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.

www.amitpatil.me
  • 3,001
  • 5
  • 43
  • 61
  • possible duplicate of [What is the best regular expression to check if a string is a valid URL?](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – user123444555621 May 13 '12 at 08:50
  • @Pumbaa80 : No my question is little different, My input could have plain string, only url, or both text and url together. – www.amitpatil.me May 18 '12 at 13:00

5 Answers5

26

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 Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • This one works, But i am trying to check it once its compltely typed i mean...when i type "http://www.go" this condition satisfies and shows alert...i want this script to wait until user enters complete url and then only show alert...by the way i m using keyup event to check. – www.amitpatil.me May 13 '12 at 08:52
  • 1
    @AmitPatil use blur event of the textbox, instead of keyup, sudhir's answer seems not extract that url, btw. – yjshen May 13 '12 at 08:56
  • you can use blur event, so that once focus is lost from the textbox you can runs the code... – Sudhir Bastakoti May 13 '12 at 08:57
  • Yes thats the trick i can think of at last, but the reason i want it on keyup event is that...whenever i detect url i want to fire ajax call to extract details of the url in the meantime user can continue his typing...like it works on facebook status – www.amitpatil.me May 13 '12 at 09:08
3

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.' ]
N Klosterman
  • 1,231
  • 14
  • 23
1

You can modify the regex to conditionally match on the scheme of the URL, like so:

var urlCheck = new RegExp('([a-zA-Z\d]+://)?(\w+:\w+@)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(/.*)?', 'i')
if (urlCheck.test(status_text) {
    console.log(urlCheck.exec(status_text));
}
Jesse Proulx
  • 821
  • 7
  • 13
1
var reg = new RegExp('([a-zA-Z\d]+://)?((\w+:\w+@)?([a-zA-Z\d.-]+\.[A-Za-z]{2,4})(:\d+)?(/.*)?)', 'i')
if (reg.test(status_text)) {
    alert(reg.exec(status_text)[2]);
}
yjshen
  • 6,583
  • 3
  • 31
  • 40
0

try this one

(?<http>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)
Netorica
  • 18,523
  • 17
  • 73
  • 108