String replace in Javascript

Taking a note:

Javascript String.replace([pattern], [replacement]) method replaces only first occurence of the matched pattern. To replace all occurences, you’ll need a “global” flag:

var str = "ABCDEFGHABCD"; //some string
alert(str.replace(/A/g), "I"); //replacing each appearance
                               //of A with an I
This entry was posted in Software Development and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *