Navigate back to the homepage

How to find consecutive repeated characters in a string in javascript?

Infinitbility
JavaScript
May 22nd, 2022 · 1 min read

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.

  1. Create samples of consecutive repeated characters strings variable.
  2. To validate consecutive repeated characters, regex expression with the test method
  3. show which characters consecutively repeated
1// Create samples consecutive repeated characters strings variable
2let str1 = "infinitbility";
3let str2 = "aaakash";
4
5// To validate consecutive repeated characters, regex expression with test method
6// show which characters consecutive repeated
7if (/(.)\1\1/.test(str1)) {
8 console.log("str1 have consecutive characters")
9} else {
10 console.log("str1 haven't consecutive characters")
11}
12
13if (/(.)\1\1/.test(str2)) {
14 console.log("str2 have consecutive characters")
15} else {
16 console.log("str2 haven't consecutive characters")
17}

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.

javascript, find consecutive repeated characters in a string example
javascript, find consecutive repeated characters in a string example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to find and remove element from array in javascript?

Example of find and remove element from array in javascript with step by step guide.

May 21st, 2022 · 1 min read

How to find the index of an object in an array in javascript?

Example of find the index of an object in an array in javascript with step by step guide.

May 21st, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]