我的标题中包含一个或多个|
的文字我想使用javascript删除它的所有实例。
我试过
$('title').text().replace(/ |g /, ' ');
"What’s New On Netflix This Weekend: March 3–5 | Lifestyle"
答案 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)