Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript regex split consecutive string (vs Java)

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"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.

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading