I need to get separate numbers + special characters + letters from a string with numbers, letters, and special characters. My attempt
var chars = str.slice(0, str.search(/[a-zA-Z]+/));
var numbs = str.replace(chars, '');
The code does not work in case there are no letters.
example of how it should work
'123.331abc' -> '123.331' + 'abc'
'12331abc123' -> '12331' + 'abc123'
3
Answers
then you just use String.prototype.substring()
Match the first part, then
.slice()
to get the second:^[^a-z]*
means 0 or more non-letter at the beginning. Due to the nature of the*
quantifier, this will always match, resulting in a match array whose first element is the first part.Try it:
You can get the both parts with a regex only: