JavaScript equivalent of PhP preg_split JavaScript equivalent of PhP preg_split php php

JavaScript equivalent of PhP preg_split


Any string in javascript can be split using the string.split function, e.g.

"foo:bar".split(/:/)

where split takes as an argument either a regular expression or a literal string.


You can use regular expressions with split.

The problem is the escape characters in the string as the (? opens a non capturing group but there is no corresponding } to close the non capturing group it identifies the string to look for as '


If you want support for all of the preg_split arguments see https://github.com/kvz/phpjs/blob/master/_workbench/pcre/preg_split.js (though not sure how well tested it is).

Just bear in mind that JavaScript's regex syntax is somewhat different from PHP's (mostly less expressive). We would like to integrate XRegExp at some point as that makes up for some of the missing features of PHP regexes (as well as fixes the many browser reliability problems with functions like String.split()).