user1130217
user1130217

Reputation:

regex remove column "|" character

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

Answers (4)

bubblez
bubblez

Reputation: 814

the regex is /\|/, you have to escape | since in regex, | is used to declare alternatives.

Upvotes: 5

Satya
Satya

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

jbabey
jbabey

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

Loktar
Loktar

Reputation: 35309

string.replace(/\|/gi,'')

Need to escape it due to it being a special character. You use \ to escape.

Live Demo

Upvotes: 3

Related Questions