Reputation: 163
I need regex pattern to split a string into numbers and letters. I.e. .1abc2.5efg3mno
should be split into [".1","abc","2.5","efg","3","mno"]
.
The current regex I tried is:
var str = ".1abc2.5efg3mno";
regexStr= str.match(/[a-zA-Z]+|[0-9]+(?:\.[0-9]+|)/g);
Output obtained is:
["1","abc","2.5","efg","3","mno"]
The number .1
is taken as 1
whereas I need it as .1
.
Upvotes: 15
Views: 22743
Reputation: 1126
var z = ".1abc2.5efg3mno".match(/[\d\.]+|\D+/g);
console.log(z);
Upvotes: 4
Reputation: 6355
If it's a matter of separating letters from non-letters, then the regex can be made quite simple:
var str = ".1abc2.5efg3mno";
var regexStr = str.match(/[a-z]+|[^a-z]+/gi);
console.log(regexStr);
I.e. match a group of letters or a group of non-letters.
Upvotes: 31