替换单引号的代码不起作用

时间:2016-07-06 08:20:36

标签: javascript html

替换代码不适用于单引号所以请帮助我。 有些地方它正在工作,有些地方它不起作用。

Javascript代码:

var demo = "Broadcasted on Zee TV from Monday to Friday, the Indian television soap opera Kumkum Bhagya has long been revolving around the mystery of Tanu’s pregnancy. Tanu, a cunning, sly and guileful character in the Kumkum Bhagya serial has been fooling Abhi, the rock star. She is actually pregnant with Nikhil, her ex-boyfriend, but claims to be the mother of Abhi’s child. Except Abhi, almost everyone in the show knows who Tanu is pregnant with. But, the mystery will remain unrevealed until Abhi knows the reality of Tanu’s pregnancy. The coming episodes of Kumkum Bhagya, Pragya's DNA disclosure is likely to reveal the secret.";
var demo1 = demo.replace(/[']/g,";");
//alert(demo1);
document.getElementById("hello").innerHTML = demo1;

HTML代码:

<div id="hello">
</div>

请以此为参考: https://jsfiddle.net/h3sujuLx/

1 个答案:

答案 0 :(得分:3)

在你的字符串中,你有unicode字符'右单引号'和撇号,但在你的正则表达式中你有一个撇号,它是一个不同的字符。如果要替换两个字符,则需要在正则表达式中包含这两个字符:

var demo1 = demo.replace(/['’]/g,";");

具体比较:Tanu’s(右单引号)和Pragya's(撇号)

为了便于输入,您可以使用unicode转义序列而不是字符:

var demo1 = demo.replace(/['\u2019]/g,";");