Hi Friends 👋,
Welcome To Infinitbility! ❤️
Today, I’m going to show you how I find consecutive repeated characters in a string, here I will use some regex expression and javascript test()
method to validate consecutive repeated characters in an array.
The test() method executes a search for a match between a regular expression and a specified string. Returns true or false.
Well, let’s start today’s topic How to find consecutive repeated characters in a string in javascript?
Okay, let’s plan what we are going to do in code.
- Create samples of consecutive repeated characters strings variable.
- To validate consecutive repeated characters, regex expression with the test method
- show which characters consecutively repeated
// Create samples consecutive repeated characters strings variable
let str1 = "infinitbility";
let str2 = "aaakash";
// To validate consecutive repeated characters, regex expression with test method
// show which characters consecutive repeated
if (/(.)\1\1/.test(str1)) {
console.log("str1 have consecutive characters")
} else {
console.log("str1 haven't consecutive characters")
}
if (/(.)\1\1/.test(str2)) {
console.log("str2 have consecutive characters")
} else {
console.log("str2 haven't consecutive characters")
}
In the above example, I’m using the regex expression to find strings that have consecutive characters.
Here, as per expectation, it should show str2 have consecutive characters
, let’s check the output.
I hope it’s help you, All the best 👍.