3

I'm using:

var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(exp,"<a href='$1' target='internet'>$1</a>"); 

to turn, for example http://www.example.com into a clickable link. This works well!

Does anyone know how to expand this expression to capture also www.example.com (i.e without http://)?

4
  • Start from the beginning. What are you trying to do? Commented Aug 12, 2011 at 21:50
  • @Ash: Seems he wants to convert the strings http://www.example.com and www.example.com to <a> tags. Commented Aug 12, 2011 at 21:52
  • yes - am trying to convert example.com and www.example.com to <a> tags. Commented Aug 12, 2011 at 21:57
  • This is nice in principle, but it won't be easy... Depending on your source, you'll probably end up getting a lot of false positives. Commented Aug 12, 2011 at 21:59

2 Answers 2

1

This worked for me:

Add a ( )* here

((https?|ftp|file):\/\/)*

http://jsfiddle.net/jasongennaro/NBWyr/

Basically zero or more of the items in ()

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

3 Comments

I tried that with var text = 'http://www.google.com is a cool website, but www.yahoo.com is not.'; and it didn't work properly. More text was highlighted than should have been.
@styfle. As far as I know, this regex is meant to work on addresses only, not sentences. I could be wrong, though.
@Jason I figured it would parse a block of text like a comment on a website, then find and replace links in that block.
0

try this regex:

(?<http>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)

1 Comment

Thanks. I'm having problems with using this in a function: function replaceWWW(text) { var exp = (?<http>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)/ig; return text.replace(exp,"<a href='$1' target='internet'>$1</a>"); } Maybe I have missed a escape character?

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.