How to Replace all Occurrences of a String in JavaScript Using Regex Expression Full Project For Beginners

 

Welcome folks today in this blog post we will be replacing all occurrences of a string in javascript using regex expression. All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

In order to get started you need to make an index.html file and copy paste the following code

 

 

index.html

 

 

var str = 'CW is the best blog, visit CW now.';

// Replace CW with CodexWorld
var res = str.replace(/\CW/g, 'CodexWorld');

 

 

So here we are using the replace() method and passing a simple regex expression to replace all occurrences of the world CW with codexworld

 

See also  Build a HTML5 Feedback Modal Form in Browser Using CSS3 and Javascript Full Project For Beginners

 

var str = 'CW is the best blog, visit CW now.';

// Replace ' ' with _
var res = str.replace(/\ /g, '_');

Leave a Reply