Monday, July 29, 2019

[JavaScript][Resolved] js string replace all

I want replace all "a" in a sentence:

<script>
var str = "I am a foo, I want a bar.";
document.write(str.replace("a","#"));
</script>


Result :



This replace the first occurrence , the first a only,
<script>
var str = "I am a foo, I want a bar.";
document.write(str.replace(/a/g,"#"));
</script>

Resulr:



No comments :

Post a Comment