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?

18th Apr 2022, 5:00 PM
kwee
7 odpowiedzi
0
If am using variable like var x =getElementById("").value how to convert it
18th Apr 2022, 5:06 PM
kwee
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 (..........., ..........);
18th Apr 2022, 5:12 PM
kwee
0
var re = /(\w+)\s(\w+)/; var str = "John Smith"; var newstr = str.replace(re, "$2, $1"); print(newstr);
18th Apr 2022, 6:55 PM
Oma Falk
Oma Falk - avatar
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 ) );
18th Apr 2022, 10:03 PM
Ipang