I'm wondering if there's a [succinct] way to split a string where ever the "class" (e.g. Unicode category, or even simply something like letters/digits/whitespace) of a character changes from one to the next.
For example, something like "18a.1.50".split(/\b/)
almost works but yields ["18a", ".", "1", ".", "50"]
("18a" considered a word) instead of ["18", "a", ".", "1", ".", "50"]
.
I'd prefer a solution in JS-compatible regular expression syntax, but I'm also curious for "regular expressions" in general.
I'm not much of a regex wizard, so there are probably better ways, but this seems to work as described.
"18a.1.50".match(/\.|\d+|[a-z]+/gi) //["18", "a", ".", "1", ".", "50"]
"18a..b12.1.50".match(/\.|\d+|[a-z]+/gi) // ["18", "a", ".", ".", "b", "12", ".", "1", ".", "50"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With