Reputation: 25
I would like to get parts of a string separated by the type (number or character)
Simplified initial situation:
var content = 'foo123bar456';
Desired result:
result = ['foo', 123, 'bar', 456];
This is what I have so far to match the first "foo"
^(([a-z])|([0-9]))+
I thought this would match either characters [a-z]+ OR numbers [0-9]+ (which would match 'foo' in this case) but unfortunately it allows both (characters AND numbers) at the same time.
If it would match only characters with the same type I could just add "{1,}" to my regex in order to match all occurrences of the pattern and the world would be a bit better.
Upvotes: 1
Views: 226
Reputation: 39355
Correct regex will be:
([a-zA-Z]+|[0-9]+)
Explanation of the regex is:
NODE EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[a-zA-Z]+ any character of: 'a' to 'z', 'A' to 'Z'
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
Upvotes: 3