I'm trying to do some basic string manipulation on a text variable in Javascript. I have a few lines of code that look like this:
var someVar = 'first+last@domain.com';
var cleanVar = someVar.replace('@','_').replace('+','_');
When I run this code in Chrome's Javascript console, the value of cleanVar
is first_last_domain.com
... which is as intended. When this code runs on the live page in the browser, the replace()
always works for th @
character, but never for the +
.
To debug this, I've tried swapping positions of the .replace('@','_')
with .replace('+','_')
and even breaking things out like this:
var someVar = 'first+last@domain.com';
var cleanVar = someVar.replace('@','_');
var cleanVar2 = cleanVar.replace('+','_');
Surely I'm missing something rudimentary here. I know the +
combines strings in Javascript... but not when surrounded by quotes, right?