Vue Js String Search Method: Vue.js search() method is used to search a string for a given text and returns the index within the string of the first match if one is found; otherwise, it will return -1. Here in this tutorial, we are going to explain how you can use this method to search for a particular character or string in a string and get the index of it Our online editor is user-friendly, easy to use, and allows you to quickly edit and run code online.
Vue Js text search in a string
In Vue.js, the search() method is used to find an element with a specific value
Vue Js Search substring of string
<div id="app">
<button @click="myFunction">click me</button>
<p>Text: {{text}}</p>
<p>First Index :{{results}}</p>
</div>
<script type="module">
import { createApp } from 'vue'
createApp({
data()
{
return{
text :'Learn Vue Js tutorials: learn vue tutorials the in easy way here. ',
searchText:'Vue',
results:''
}
},
methods:{
myFunction(){
this.results = this.text.search(this.searchText);
},
}
}).mount('#app')
</script>
Output of above example
In Vue Js, find the first index (position) character of a string
The search method in Vue Js is useful for finding certain patterns within strings, such as a particular word or phrase.
Vue Js Search character of String | Example
<div id="app">
<button @click="myFunction">click me</button>
<p>Text: {{text}}</p>
<p>First Index :{{results}}</p>
</div>
<script type="module">
import { createApp } from 'vue'
createApp({
data()
{
return{
text :'With Vue tutorials becoming increasingly popular, its important to learn how to use the framework effectively ',
results:''
}
},
methods:{
myFunction(){
this.results = this.text.search('Vue');
},
}
}).mount('#app')
</script>