1

I'm pretty unfamiliar with RegExp, and Im trying to implement it to detect urls in strings. The one regexp I want to use is this one (please don't provide your own):

/(((http|ftp|https)://)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?)/g

And replace all the matches with this:

<a href="$1">$1</a>

If you visit http://gskinner.com/RegExr/ with that example and use it in the replace, you'll see it works flawlessly, but it's impossible to build a working solution with this in javascript.

var text = "hi, demo link http://stackoverflow.com is a great website"
//regexp magic


//expected result:
textWithLink ="hi, demo link <a href="http://stackoverflow.com">http://stackoverflow.com</a> is a great website"
6
  • 1
    How have you tried to express that regex in JS syntax? How have you tried to use the resulting regex object? Commented Feb 26, 2013 at 15:22
  • 1
    @Jack you have to escape the forward slashes within the regex Commented Feb 26, 2013 at 15:24
  • @JanDvorak Hmm, I assumed that wasn't necessary when you do var re = new RegExp('(((http|ftp|https)://)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:/~+#-]*[\w@?^=%&amp;/~+#-])?)', 'g'); Commented Feb 26, 2013 at 15:29
  • @Jack that's correct, but you normally create regexes as /.../g Commented Feb 26, 2013 at 15:31
  • @JanDvorak That's just an alternative writing style, I don't think that should really matter, except for the fact that you have to escape the forward slash when the delimiter is already that. Commented Feb 26, 2013 at 15:32

3 Answers 3

2
var str = 'hi, demo link http://stackoverflow.com is a great website',
    regex = /(((http|ftp|https):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?)/g;
str.replace(regex, '<a href="$1">$1</a>');

Note: It's your exact regex, I just escaped the forward slashes.

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

1 Comment

Thanks to all, I now understand much better how to use regex in js. Awesome!
1

You can do this

var ok = /(((http|ftp|https):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:/~+#-]*[\w@?^=%&amp;/~+#-])?)/.test(str);

to test if a string is an URL.

If you want to look for URLs in a string, use

var matches = str.match(/(((http|ftp|https):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:/~+#-]*[\w@?^=%&amp;/~+#-])?)/g);

Note that it's a regex literal and that I had to escape the /.

1 Comment

@JanDvorak I already answered my own question, I'll delete my comments.
1

The RegExp literal you provide is invalid:

/(((http|ftp|https)://)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:/~+#-]*[\w@?^=%&amp;/~+#-])?)/g
^                   ^^
Start               |\__ Error
                    |
                   End

It works in the link you provide because that application strips delimiters automatically and inadvertently fixes the error.

Comments

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.