0

I am looking to detect last url from text using javascript or mootools. Url canbe without prefix/scheme

I am working on URL auto sense like Facebook. Where a user may give an URL www.example.com or with http://www.example.com either of them should be detected by JavaScript. see stackoverflow detected URL that included with scheme without URL scheme it couldn't detect URL. In my case I need both.

Here is some text

'http://www.example.com www.example2.com'

Now I want www.example2.com It will be better if I get full array containing both http://www.example.com and www.example2.com

I searched a lot but couldn't find solution. Most close to my requirements were Question about URL Validation with Regex and How do I extract a URL from plain text using jQuery?

Any help greatly appreciated.

Community
  • 1
  • 1
ARIF MAHMUD RANA
  • 5,026
  • 3
  • 31
  • 58

3 Answers3

1

REGEX

/([^:\/?# ]+:)?(\/\/[^\/?# ]*)?[^?# ]+(\?[^# ]*)?(#\S*)?/gi

**SAMPLE CODE**
var str = 'http://www.example.com www.example2.com scheme://username:password@domain:port/path?query_string#fragment_id';
var t = str.match(/([^:\/?# ]+:)?(\/\/[^\/?# ]*)?[^?# ]+(\?[^# ]*)?(#\S*)?/gi);

/* 
   t contains : 
   [ 
     "http://www.example.com",
     "www.example2.com",
     "scheme://username:password@domain:port/path?query_string#fragment_id"
   ]
*/

**DEMO** >http://jsfiddle.net/wvYTd/
**DISCUSSION**
  • This regex will find any substring that looks like an URL in an input string.
  • No validation is performed on any URL found. For instance, if the input string is 3aBadScheme://hostname, the regex will detect it as an URL. In this example, 3aBadScheme is invalid since a scheme MUST start with a letter.

    Excerpt from RFC3986

(...)
Scheme names consist of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-").
(...)

Community
  • 1
  • 1
Stephan
  • 41,764
  • 65
  • 238
  • 329
1

Given your input string, I think you just want to split it using spaces as separator? .split(' ') ?

Savageman
  • 9,257
  • 6
  • 40
  • 50
1

by combing info in these 2 links: How do I extract a URL from plain text using jQuery? Detect URLs in text with JavaScript

We can get this: http://jsfiddle.net/qQwGA/1/

If I understand what you're trying to do, this should cover it.

Community
  • 1
  • 1
Pyro979
  • 202
  • 1
  • 10