开发者

Javascript Replace Regex

开发者 https://www.devze.com 2023-02-01 02:00 出处:网络
Ok, I\'m actually trying to replace text. Basically, I am needing to replace all instances of this:| with a blank string \'\'

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('');
0

精彩评论

暂无评论...
验证码 换一张
取 消