Javascript examples for String:replace
Using a function to return the replacement text:
<!DOCTYPE html> <html> <body> <p id="demo">blue red pink.</p> <button onclick="myFunction()">Test</button> <script> function myFunction() {/*from ww w . j ava2s.com*/ var str = document.getElementById("demo").innerHTML; var res = str.replace(/blue|red|pink/gi, function myFunction(x){return x.toUpperCase();}); document.getElementById("demo").innerHTML = res; } </script> </body> </html>