0
Javascript how to replace a word instead of a character
I want to replace a word instead of a word using javascript How do i solve this?
7 Answers
0
If am using variable like var x =getElementById("").value how to convert it
0
For example,
var x = document.getElementById("fulltext").value;
var y = document.getElementById("find").value;
var z = document.getElementById("replace").value;
x = x.textContent.replace (..........., ..........);
0
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
print(newstr);
0
* How to use a variable's content in a regular expression
https://stackoverflow.com/questions/494035/how-do-you-use-a-variable-in-a-regular-expression
* Adapted snippet:
var haystack = document.getElementById("fulltext").value;
var needle = document.getElementById("find").value;
var newNeedle = document.getElementById("replace").value;
var expr = new RegExp( needle, "gi" );
console.log( haystack.replace( expr, newNeedle ) );