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

Answers


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)


Need Your Help

events on arrayproxy emberjs

javascript ember.js

So I have an object of ArrayProxy kind

iOS WatchKit / Swift - Lazy Loading images in WKInterfaceTable rows

ios swift watchkit user-experience apple-watch

In my Apple Watch app, I have a table with about 25 rows, and each one has a few bits of text and an image that needs to be loaded from the internet. Similar to an Instagram-style feed, but these are

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.