Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string wherever character class changes

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.

like image 407
natevw Avatar asked Oct 01 '22 06:10

natevw


1 Answers

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"]
like image 193
brbcoding Avatar answered Oct 10 '22 04:10

brbcoding