Answers for "js replace word in string"

64

javascript replace string

var str = "JavaScript replace method test";
var res = str.replace("test", "success");  
//res = Javscript replace method success
Posted by: Guest on July-10-2020
0

how to replace word from string in javascript

var str = "Please locate where 'locate' occurs!";

str.replace("locate", "W3Schools"); //replace only replace first match from string
str.replace(/LOCATE/i, "W3Schools"); // i makes it case insensitive
str.replace(/LOCATE/g, "W3Schools"); // g replace all matches from string rather than replacing only first

document.write("<br>" + "replace:", res7);
document.write("<br>" + "replace with case insensitive:", res8);
document.write("<br>" + "replace all:", res9);
Posted by: Guest on June-04-2021
26

replace all occurrences of a string in javascript

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

console.log(p.replaceAll('dog', 'monkey'));

// expected output: "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"
Posted by: Guest on July-25-2020
6

str replace javascript all

str.replace(/abc/g, '');
Posted by: Guest on May-16-2020

Code answers related to "js replace word in string"

Code answers related to "Javascript"

Browse Popular Code Answers by Language