Javascript Replace Regex
Ok, I'm actually trying to replace text.
Basically, I am needing to replace all instances of this: |
with a blank string ''
However, this isn't working:
langName = langName.replace(/|/g, '');
Also, would be best if I could also replace all of these instances within the string, with a ''
also:
"
double quote
'
single quote
/
back slash
\
forward slash
And any other html entity characters. Arrggg.
Can someone please help me her开发者_Go百科e? Perhaps it can be turned into a String.prototype
function so I can use it more than once?
Thanks :)
You need to escape |
with \
like:
langName = langName.replace(/\|/g, '');
Test Case:
var langName = 'this| is | some string';
langName = langName.replace(/\|/g, '');
alert(langName);
Output:
this is some string
The reason why you need to escape |
is that it is special regex character.
Alternatively, you could also use split
and join
like this:
langName = langName.split('|').join('');
精彩评论