Javascript删除所有'|'在一个字符串中

时间:2017-03-02 11:51:20

标签: javascript

我的标题中包含一个或多个|的文字我想使用javascript删除它的所有实例。

我试过

$('title').text().replace(/ |g /, ' ');
"What’s New On Netflix This Weekend: March 3–5 | Lifestyle"

2 个答案:

答案 0 :(得分:7)

你的正则表达式无效。

有效的看起来像这样:

/\|/g

在普通的JS中:

var text = "A | normal | text |";
var final = text.replace(/\|/g,"");
console.log(final);

答案 1 :(得分:1)

RegEx不正确,它应该如下所示,最后有g开关,|已转义。



var str = "What’s New On Netflix |This Weekend: March 3–5 | Lifestyle"

var replaced = str.replace(/\|/g, '');

console.log(replaced)