Wednesday, September 21, 2016

JavaScript: Replacing All Instances of a Character

Let's assume that we want to replace all hyphens with blank spaces.

We can either loop through the string character by character until we replace all hyphens,
or we can create a regular expression as follows:

var myStr = 'This-is-a-test';
var replacedString = myStr.replace(/-/g, ' ');
Where g represents a global match.

No comments:

Post a Comment