Given a string in Javacript, such as
var str = \"this\'s kelly\";
I want to replace the apostrophe (\') with another character. Here is what I\
Do this:
str = str.replace("'","A");
str = str.replace("'", "A");
Your running the function but not assigning it to anything again so the var remains unchanged
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');