0

I'm trying to search plain old strings for urls that begin with http, but all the regex I find doesn't seem to work in javascript nor can I seem to find an example of this in javascript.

This is the one I'm trying to use from here and here:

var test = /\b(?:(?:https?|ftp|file)://www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]/;

But when I try to run it, I get "Unexpected token |" errors.

Community
  • 1
  • 1
AdamB
  • 3,101
  • 4
  • 34
  • 44

2 Answers2

1

Ok, a comment seems to be not enough, hard to find full answer. I rewrite whole proper regexp: (tested, it works good)

var test = /\b(?:(?:https?|ftp|file):\/\/www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i;

The i on the end means 'ignore case', so it is necessary for this regexp.

pepkin88
  • 2,742
  • 20
  • 19
0

You're using / as your regex delimiter, and are also using / within the regex (before www), so the regex actually terminates after the first / before www. Change it to:

var test = /\b(?:(?:https?|ftp|file):\/\/www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]/;
                                     ^^^^ escape here
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • forgot to mention, this regexp is case insensitive, so you would rather write this: `var test = /\b(?:(?:https?|ftp|file):\/\/www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i;` – pepkin88 Sep 07 '10 at 02:08