Created
September 4, 2019 17:28
-
-
Save masautt/c27efdded65c976f7dc19cef9c6a8e2c to your computer and use it in GitHub Desktop.
How to replace all occurrences of a string in JavaScript?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using RegEx | |
String.prototype.replaceAll = function(search, replacement) { | |
var target = this; | |
return target.replace(new RegExp(search, 'g'), replacement); | |
}; | |
// Split and Join (Functional Implementation) | |
String.prototype.replaceAll = function(search, replacement) { | |
var target = this; | |
return target.split(search).join(replacement); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment