I have this splitting regex to group consecutive words in String
/(?<=(.))(?!\1)/g
So in Java the above regex would split the string as I'm expected like this
"aaabbbccccdd".split("(?<=(.))(?!\\1)");
// return [aaa, bbb, cccc, dd]
but in JS the same regex will split like this, included my capture group 1
'aaabbbccccdd'.split(/(?<=(.))(?!\1)/g);
/// return ['aaa', 'a', 'bbb', 'b', 'cccc', 'c', 'dd']
So is there anyway to avoid capture group in result in JS (I've tried (?:...) but then I can't use \1 anymore)
And if you have other way or improvement to split consecutive words in JS (using or not using regex), please share it, thank you.
CodePudding user response:
I would keep your current logic but just filter off the even index list elements:
var input = "aaabbbccccdd";
var matches = input.split(/(?<=(.))(?!\1)/)
.filter(function(d, i) { return (i 1) % 2 != 0; });
console.log(matches);