How To Split A String On Pattern Of One Or More Repeating Character And Retain Match?
Solution 1:
Solutions 2 and 3 are equal because unanchored lookaheads test each position in the input. string. (?=a) tests the start of string in abaacaaa, and finds a match, the leading empty result is discarded. Next, it tries after a, no match since the char to the right is b, the regex engine goes on to the next position. Next, it matches after b. ab is added to the result. Then it matches a position after a, adds a to the resulting array, and goes to the next position to find a match. And so on. With (?=a+) the process is indetical, it just matches 1+ as, but still tests each position.
To split babaacaaa, you need
var s = 'babaacaaa';
console.log(
s.split(/(a+[^a]*)/).filter(Boolean)
);
The a+[^a]* matches
a+- 1 or morea[^a]*- 0 or more chars other thana
The capturing group allows adding matched substrings to the resulting split array, and .filter(Boolean) will discard empty matches in between adjoining matches.
Solution 2:
let string = 'abaacaaa'
let result = string.match(/a*([^a]+|a)/g)
console.log(result)
string = 'babaacaaa'
result = string.match(/a*([^a]+|a)/g)
console.log(result)
Solution 3:
string.match(/^[^a]+|a+[^a]*/g) seems to work as expected.
Post a Comment for "How To Split A String On Pattern Of One Or More Repeating Character And Retain Match?"