How can i change regex to work in javascript
-
Hello I have regex expression that is work in asp.net for finding link
SET objRegex = NEW regexp
objRegex.Pattern = "(\b(?:(?:https?|ftp|file)://|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$]))"Now i want to use this expression in javascript. how can i write ? I tried with this but not working.
var re = new RegExp(/\b(/?:(/?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])/g);
Is there any Converter available for this?
“Before you start some work, always ask yourself three questions - Why am I doing it, What the results might be and Will I be successful. Only when you think deeply and find satisfactory answers to these questions, go ahead.” -Chanakya Thanks Adit
-
Hello I have regex expression that is work in asp.net for finding link
SET objRegex = NEW regexp
objRegex.Pattern = "(\b(?:(?:https?|ftp|file)://|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$]))"Now i want to use this expression in javascript. how can i write ? I tried with this but not working.
var re = new RegExp(/\b(/?:(/?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])/g);
Is there any Converter available for this?
“Before you start some work, always ask yourself three questions - Why am I doing it, What the results might be and Will I be successful. Only when you think deeply and find satisfactory answers to these questions, go ahead.” -Chanakya Thanks Adit
The forward slashes are causing your problems because they're telling the interpreter that it's the end of the expression since they're not escaped. Converting your expression from the original into the javascript should only require a copy/paste and manually escaping the pattern's forward slashes. Ie,
(?:https?|ftp|file)://|www\.|ftp\.)
becomes(?:https?|ftp|file):\/\/|www\.|ftp\.)
Here's a Regex reference for javascript that can help you convert your expression: http://www.w3schools.com/jsref/jsref_obj_regexp.asp[^] Also, there's a regular expression forum on here: http://www.codeproject.com/Forums/1580841/Regular-Expressions.aspx[^] They will probably give you more help about regex syntax problems than the javascript forum.