Vue.js String Replace


We can use native javascript string replace function to replace a value in string. Here in this tutorial, we are going to explain how you can use replace method in VueJS. You can also use our online editor to edit and run the code online.


Vue.js String Replace Example

Here is an example of replace function in vue.js. –

Example:

<!DOCTYPE html>
<html>
<head>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>
</head>
<body>
<div id="app">
	<div id="myId" ref="myId">{{ message }}</div>
	<button @click="myFunction()">Click Me</button>
</div>
<script>
 new Vue({
el: '#app',
  
  data: { 
	  message:"Welcome to Canada!"
  },
  
   methods:{
    myFunction: function () {
		
		this.message = this.message.replace("Canada", "France");
		
    }
	}

});
</script>        
</body>
</html>

Try it »

On the same way you can use this function to replace a value or pattern.

Output of above example-

Vue.Js String Replace function example

Advertisements

Add Comment

📖 Read More