Reputation: 8374
Assume I have this string cde123abc
, I want it to split into followings:
cde
, 123
, abc
.
The above string is just an example, please imagine any string which contains characters and numbers.
How to do that? Regular expression??
Upvotes: 1
Views: 97
Reputation: 18825
Assuming you want to split to letters and numbers, the following should work:
while ((m = str.match (/^(\d+|[a-zA-Z]+)(.*)/)) {
result.push (m [0]);
str = m [1];
}
Upvotes: 1
Reputation: 15846
Use regular expression match()
.
alert('cde123abc'.match(/[a-zA-Z]+|[0-9]+/g))
Upvotes: 4
Reputation: 66488
If you always have letters numbers letters then this should work:
var str = 'cde123abc';
var result = str.split(/([0-9]+)/);
alert(JSON.stringify(result));
Upvotes: 3