views:

64

answers:

3

I have this line of code in javascript

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

Usually I encapsulate the regex syntax with the / characters but since they are found within the regex it screws up the encapsulation. Is there another way how I can store it inside the variable?

The current slashes that seem like escape characters are part of the regex, since I am using this in c# aswell and works perfectly

+4  A: 
var re = new RegExp("^your regexp.*$", "gi");
geowa4
+1  A: 

You can escape the slash inside your regex:

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

(you already did so with the first two slashes...)

Tim Pietzcker
+3  A: 

One way is to escape all occurances of / in your regex as \/, like you're already partially doing:

var re = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?/;
Ates Goral
Worked as you said
Drahcir