Navigate back to the homepage

How do you check if a letter is repeated in a string in Javascript?

Infinitbility
JavaScript
October 18th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To check if a letter is repeated in a string in javascript, use the test() method with /(.).*\1/ regex it is the simplest way to check string has a duplicate characters or not.

Today, I’m going to show you How do I check if a letter is repeated in a string in javascript, as mentioned above, I’m going to create a function that checks string contains a duplicate character or not.

Let’s start today’s tutorial on how do you check if a letter is repeated in a string in javascript.

Javascript checks if a letter is repeated in a string

Here, we will do

  1. Create a function which checks duplicate letter
  2. Use the test() method with regex in a function to check repeated letter
  3. Check the function in sample strings
1// Create a function which checks duplicate letter
2function hasRepeats(str) {
3
4 // Use `test()` method with regex in a function to check repeated letter
5 return /(.).*\1/.test(str);
6}
7
8// Check function in sample strings
9
10function hasRepeats (str) {
11 return /(.).*\1/.test(str);
12}
13
14console.log(hasRepeats("infinitbility.com")) // true
15
16console.log(hasRepeats("aguidehub.com")) // true
17
18console.log(hasRepeats("sortoutcode.com")) // true
19
20console.log(hasRepeats("abcdefg")) // false

Output

I hope it helps 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 do you separate vowels and consonants in a string in Javascript?

An example of checking duplicate characters in a string with the step-by-step guide.

October 18th, 2022 · 1 min read

How to store multiple strings in an array in Javascript?

An example of store multiple strings in an array javascript with the step-by-step guide.

October 17th, 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]