Javascript regex to match repeatable alphabet

I need Regex in Javascript to match repeatable alphabet.

I want to specify a duplicate letter in a string using a regular expression. As well as a split around them.

var str = "aabbcde"; str.split(/[az](?=$1)/g) 

but it still returns a whole string. I want to split ["aabbcde"]. How to make a regular expression pattern match duplicate letters? What I tried is a match with one alphabet first and what follows from a match using (? = Regex. But this is not wokr. Any idea? Thanks a lot

My output result will be

  var str = "aabbcde"; str.split(/[az](?=$1)/g) // output = ["aa", "bb", "c", "d", "e"] 
+4
source share
1 answer

You must surround the regex () to create a group. And use \1 to refer back to the group (instead of $1 $1 can be used in the replacement string in the replace method).

 var str = "aabbcde"; str.match(/([az])\1*/g) // => ["aa", "bb", "c", "d", "e"] 
+6
source

Source: https://habr.com/ru/post/1502230/


All Articles