JavaScript Program to Replace Characters in all Occurences of a String Using replace() & Regular Expression Full Project For Beginners

 

 

script.js

 

 

// program to replace a character of a string

const string = 'Mr Red has a red house and a red car';

// replace the characters
const newText = string.replace('red', 'blue');

// display the result
console.log(newText);

 

 

// program to replace a character of a string

const string = 'Mr Red has a red house and a red car';

// regex expression
const regex = /red/g;

// replace the characters
const newText = string.replace(regex, 'blue');

// display the result
console.log(newText);

Leave a Reply