Aaron Shen
Aaron Shen

Reputation: 8374

How to split string starting from a certain place?

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

Answers (3)

Zbynek Vyskovsky - kvr000
Zbynek Vyskovsky - kvr000

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

rrk
rrk

Reputation: 15846

Use regular expression match().

alert('cde123abc'.match(/[a-zA-Z]+|[0-9]+/g))

Upvotes: 4

jcubic
jcubic

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

Related Questions