Split a string in JavaScript with regex

October 7, 2014

You can use regular expressions to split a string in JavaScript:

js
var s = "a111b22c599";
s.split(/([a-z])/); => ["", "a", "111", "b", "22", "c", "599"]

Note that the first element of the returning array is an empty string.