Perform a global replacement: - Javascript RegExp

Javascript examples for RegExp:Modifier

Description

Perform a global replacement:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p id="demo">this is a test blue red yellow</p>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/* ww w  .  j  a v  a2 s. c o m*/
    var str = document.getElementById("demo").innerHTML;
    var res = str.replace(/blue/g, "red");
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

Related Tutorials