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.

0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Pyro979 thanks it seems to work let me check a little bit more. Once again many many thanks for your response.
1

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

2 Comments

Savageman this is just a sample please keep in mind. I have given references also which were very close to my necessity. thanks.
Please edit your question with more details. I can't understand better than I answered...
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 ("-").
(...)

6 Comments

I bet that was just a sample url ;))
And customizing the answer for her need will be a good exercide for OP :)
I agree with @MichalKlouda. This is just a sample text please keep in mind. I have given references also which were very close to my necessity. thanks.
I have updated the regex and the jsfiddle link. Again, if you have more url samples valid in your context, send them.
@Stephan thanks. Let me explain me a bit more. I am working on URL auto sense like Facebook. Where a user may give an URL as I said www.example.com or with example.com either of them should be detected by JavaScript.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.