How to replace an apostrophe in a string in Javascript?-Collection of common programming errors
var str = "this's kelly"
str = str.replace(/'/g, 'A');
The reason your version wasn’t working is because str.replace
returns the new string, without updating in place.
I’ve also updated it to use the regular expression version of str.replace
, which when combined with the g
option replaces all instances, not just the first. If you actually wanted it to just replace the first, either remove the g
or do str = str.replace("'", 'A');