Industrial
Industrial

Reputation: 42778

Line-breaking a regexp in javascript?

As readability is somewhat absent in the below shown Regular expression literal, I would like to split it up over several lines. How would I do this and what does need to change/be escaped?

regex = /\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;

Thanks

Upvotes: 1

Views: 130

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336208

JavaScript doesn't support multiline regexes. You could construct a regex from a string, though:

r = 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")

(I hope I got the string building syntax correct - I'm not a JavaScript person, so if this doesn't work, let me know)

Upvotes: 1

Related Questions