Javascript examples for String:replace
The replace() method replaces string by a string or a regular expression.
To replace all occurrences, use the global (g) modifier.
This method does not change the original string.
string.replace(searchValue, newValue);
Parameter | Description |
---|---|
searchvalue | Required. The value, or regular expression, that will be replaced by the new value |
newvalue | Required. The value to replace the search value with |
A new String, where the specified value(s) has been replaced by the new value
convert "blue", "red" and "yellow" to upper-case
<!DOCTYPE html> <html> <body> <p id="demo">Blue blue hour hourse red blue car.</p> <button onclick="myFunction()">Test</button> <script> function myFunction() {/*from w w w. ja v a 2 s. c o m*/ var str = document.getElementById("demo").innerHTML; var res = str.replace(/blue|red|yellow/gi, function myFunction(x){return x.toUpperCase();}); document.getElementById("demo").innerHTML = res; } </script> </body> </html>