Nick Nick - 2 months ago 21
Javascript Question

split array regex (a but not ab, without losing char after a)

I've got the following

var example = "caty not caby";

which returns

[ "c", "y not caby" ]

but I'm trying to get

["c","ty not caby"]

How could I split the string at every 'a' but not every 'ab' without losing the char after the 'a' (which in the example above is 't')?


Other answers have mentioned negative lookahead but I'll try to explain why you need to use it.

Your regex a[^b] matches a and the next character, as long as it's not b, so it's always going to be two. In your example string, that is "caty not caby" because at matches both of those.

Lookahead patterns, however are called zero width because they do a check but are not considered part of the match, so a(?!b) will match exactly one character, even though the pattern used both a and b.

caty not caby
 ^^       ^
 ||       checked and rejected
 |checked but not considered part of the match
 character matched because the lookahead test passed

So, this is why this works.

var example = "caty not caby";
var split = example.split(/a(?!b)/)