Skip to content Skip to sidebar Skip to footer

RegularExpression To Extract Url For Javascript

I have a regular expression that works to extract a url from a given string. It's in C# and I want to convert it to javascript: private static Regex urlPattern = new Regex(@'(?i)\

Solution 1:

(?i) is no valid option to set the ignoreCase flag in JavaScript (while ignored in Opera, it seems to throw a SyntaxError for you). The flags are only given as a suffix of the regular expression literal, or as a string in the second parameter of the RegExp constructor.

Also, you forgot to escape the slashes - since the delimit the literal, they need to be escaped.

Use either

var regexToken = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'"".,<>?«»“”‘’]))/i;

or (slightly more complicated)

var regexToken = new RegExp("\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\"\".,<>?«»“”‘’]))", "i");

Post a Comment for "RegularExpression To Extract Url For Javascript"