How to check if a letter is repeated in a string in VueJS?

Hi Friends đź‘‹,

Welcome To SortoutCode! ❤️

To check if a letter is repeated in a string in VueJS. We are going to use the test() method with the /(.).*\1/ regex to check if the letter is duplicated or not.

Short solution

parentComp.vue

console.log(/(.).*\1/.test('Hii, This is Sortoutcode'));   //true

console.log(/(.).*\1/.test('abcd'));   //false

Today, I am going to show you How you check if a letter is repeated in a string in VueJS?

Table of Content

  • Setup the Vue (Optional)
  • Create FirstComponent.vue and import it into App.js
  • How to check if a letter is repeated in a string?

This article will guide you to How do I check if a letter is repeated in a string in vueJS.

Setup the Vue.js

First, we have to install the Vue project, I had installed the vueJS in my system. If you haven’t installed or have any problem with installation you can follow this article, it will show you step by step process of installation.

How to Install the VueJS project?

Create FirstComponent.vue and import it into App.js

Create the new component in the src/components/ components folder. The component name is FirstComponent.vue and imported into the App.js file:

App.js

<template>
  <FirstComp />
</template>

<script>
import FirstComp from "./components/FirstComp.vue";
export default {
  name: "App",
  components: {
    FirstComp: FirstComp,
  },
};
</script>

How to check if a letter is repeated in a string?

To check if the letter is repeated in string in VueJS. We are going to use the test() method with regex expression /(.).*\1/. Let’s take a short example to better understand.

FirstComp.vue

<template>
  <h1>Welcome to Sortoutcode.com</h1>
  <p>String1 : {{ string1 }}</p>
  <p>String2 : {{ string2 }}</p>
  <p>There are repeated letter in string1 : {{ serchString1 }}</p>
  <p>There are repeated letter in string2 : {{ serchString2 }}</p>
  <button @click="findRepeatedS1">Find String1</button>
  <button @click="findRepeatedS2">Find String2</button>
</template>

<script>
export default {
  name: "FirstComp",
  data() {
    return {
      string1: "sortoutcode",
      string2: "abcdif",
      serchString1: "",
      serchString2: "",
    };
  },
  methods: {
    findRepeatedS1() {
      this.serchString1 = /(.).*\1/.test(this.string1);
    },
    findRepeatedS2() {
      this.serchString2 = /(.).*\1/.test(this.string2);
    },
  },
};
</script>

For now, let’s check the output.

Output

check repeated letter in string

Links

Here, is the above program code sandbox link of how to check if a letter is repeated in a string in vue js. Then you can use it whenever you went and do the changes as per your requirements.

Codesandbox

All the best đź‘Ť.

Follow me on Twitter