How to separate vowels and consonants from strings in VueJS?
May 06, 2023Hi Friends đź‘‹,
Welcome To SortoutCode! ❤️
To separate vowels and consonants from strings in VueJS. There are several methods to separate vowels and consonants from strings, we are going to use the regex
and string.match()
methods.
Short solution
parentComp.vue
var Strg = 'sortoutcode';
var vowols = Strg.match(/[aeiou]/gi);
console.log(vowols) // [ "o", "o", "u", "o", "e" ]
var consonants = Strg.match(/[^aeiou]/gi);
console.log(consonants) // [ "s", "r", "t", "t", "c", "d" ]
Today, I am going to show you How you separate vowels and consonants from strings in VueJS?
Table of Content
- Setup the Vue (Optional)
- Create FirstComponent.vue and import it into App.js
- Separate vowels and consonants from string
This article will guide you to How do I separate vowels and consonants from strings 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>
Separate vowels and consonants from string
To separate vowels and consonants from strings in vuejs. We are going to define the string strg
in the data property. And we are going to Separate vowels and consonants from strings using the regular expression. For vowels, we are going to use this regex with the strg.match(/[aeiou]/gi)
method, and for the consonants we are going to use this regex with strg.match(/[^aeiou]/gi)
method.
Let’s take a short example to better understand.
FirstComp.vue
<template>
<div class="app-data">
<h1>Welcome to sortoutcode.com</h1>
<div>
<p>Array : {{ Arry }}</p>
<p v-if="vowels">Vowels: {{ vowels }}</p>
<p v-if="consonants">Consonants: {{ consonants }}</p>
<button @click="convertMethodA">Convert</button>
</div>
</div>
</template>
<script>
export default {
name: "FirstComp",
data() {
return {
strg: "sortoutcode",
vowels: "",
consonants: "",
};
},
methods: {
convertMethodA() {
this.vowels = this.strg.match(/[aeiou]/gi);
this.consonants = this.strg.match(/[^aeiou]/gi);
},
},
};
</script>
For now, let’s check the output.
Output
Links
Here, is the above program code sandbox link of how to separate vowels and consonants from strings in vue js. Then you can use it whenever you went and do the changes as per your requirements.
All the best 👍.