Reputation:
How do I remove all the occurrences of the character |
from a string with regex? I tried
string.replace(/|/gi,'');
but this does not seem to work...
Any help?
Upvotes: 0
Views: 372
Reputation: 814
the regex is /\|/
, you have to escape |
since in regex, |
is used to declare alternatives.
Upvotes: 5
Reputation: 8881
I just tried this :
<script language="javascript">
document.write('A|A');
document.write('A|A'.replace('|','B'));
</script>
And the output is what you are looking for:
A|AABA
Upvotes: 1
Reputation: 46647
The pipe has special meaning in a regular expression, you need to escape it:
string.replace(/\|/g,'');
On a side note, you don't need to ignore casing when you're not dealing with letters.
Upvotes: 2