String replace







? Il replace() metodo sostituisce un valore specificato con un altro valore in una stringa:

https://www.w3schools.com/js/tryit.asp?filename=tryjs_string_replace

<!DOCTYPE html>
<html>

<body>

<h2>JavaScript String Methods</h2>

<p>Replace "Microsoft" with "W3Schools" in the paragraph below:</p>

<button onclick="myFunction()">Try it</button>

<p id="demo">Please visit Microsoft!</p>

<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace("Microsoft","W3Schools");
document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>


? Utilizzare un?espressione regolare senza distinzione tra maiuscole e minuscole per sostituire Microsoft con W3Schools in una stringa:

https://www.w3schools.com/js/tryit.asp?filename=tryjs_regexp_string_replace

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Regular Expressions</h2>

<p>Replace "microsoft" with "W3Schools" in the paragraph below:</p>

<button onclick="myFunction()">Try it</button>

<p id="demo">Please visit Microsoft and Microsoft!</p>

<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace(/microsoft/i,"W3Schools");
document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>













( stringreplace.html )- by Paolo Puglisi - Modifica del 17/12/2023