I am in need for a regex in Javascript. I have a string:
'*window.some1.some\.2.(a.b + ")" ? cc\.c : d.n [a.b, cc\.c]).some\.3.(this.o.p ? ".mike." [ff\.]).some5'
I want to split this string by periods such that I get an array:
[
'*window',
'some1',
'some\.2', //ignore the . because it's escaped
'(a.b ? cc\.c : d.n [a.b, cc\.c])', //ignore everything inside ()
'some\.3',
'(this.o.p ? ".mike." [ff\.])',
'some5'
]
What regex will do this?
Fiddle: http://jsfiddle.net/66Zfh/3/
Explanation of the RegExp. Match a consecutive set of characters, satisfying:
There’s a difference between
+and+?. A single plus attempts to match as much characters as possible, while a+?matches only these characters which are necessary to get the RegExp match. Example:123 using \d+? > 1 and \d+ > 123.The
String.matchmethod performs a global match, because of the/g, global flag. Thematchfunction with thegflag returns an array consisting of all matches subsequences.When the
gflag is omitted, only the first match will be selected. The array will then consist of the following elements: