Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单报价的替换代码不起作用_Javascript_Html - Fatal编程技术网

Javascript 单报价的替换代码不起作用

Javascript 单报价的替换代码不起作用,javascript,html,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 guilef

替换代码不适用于单个报价,请帮助我。 有些地方它在工作,有些地方它不工作

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>

请将此作为参考:

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

var demo1 = demo.replace(/['’]/g,";");
具体比较:
Tanu的
(右单引号)和
Pragya的
(撇号)

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

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

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

var demo1 = demo.replace(/['’]/g,";");
具体比较:
Tanu的
(右单引号)和
Pragya的
(撇号)

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

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

示例文本和替换函数中使用的字符不相同。它们都是撇号,它们都有不同的字符代码,因此函数找不到匹配项。但是亲爱的,我想在同一个文档中处理这两个字符,因为当我在java中使用request.getParameter方法获取该字符串时,撇号从字符串中消失。那么,告诉我如何处理这个问题?我认为@Duncan的解决方案会起作用。在示例文本和替换函数中使用的字符不相同。它们都是撇号,它们都有不同的字符代码,因此函数找不到匹配项。但是亲爱的,我想在同一个文档中处理这两个字符,因为当我在java中使用request.getParameter方法获取该字符串时,撇号从字符串中消失。那么,告诉我如何处理这个问题?我认为@Duncan的解决方案会起作用。仔细看字符串,这两个字符看起来很相似,但并不完全相同。我在我的答案中添加了两个从你的问题中剪切和粘贴的单词,这样你可以更容易地进行比较。当我在jsp页面中编写这个新的上面的替换代码时,当我在源代码中检查该代码时,我得到了替换(/替换)™/g、 “&apos;”)而不是replace(/[']/g,“&apos;”)@SaurabhAren听起来您在将unicode字符输入源代码时遇到了问题。为了避免出现问题,只需使用转义序列\u2019(请参见编辑),我想用标准的单倒逗号替换引号和撇号。你有什么相关的解决方案吗?Thanx Duncan,你的努力对我来说是宝贵的。仔细看字符串,这两个字符看起来很相似,但并不完全相同。我在我的答案中添加了两个从你的问题中剪切和粘贴的单词,这样你可以更容易地进行比较。当我在jsp页面中编写这个新的上面的替换代码时,当我在源代码中检查该代码时,我得到了替换(/替换)™/g、 “&apos;”)而不是replace(/[']/g,“&apos;”)@SaurabhAren听起来您在将unicode字符输入源代码时遇到了问题。为了避免出现问题,只需使用转义序列\u2019(请参见编辑),我想用标准的单倒逗号替换引号和撇号。你有什么解决办法吗?邓肯,你的努力对我来说是宝贵的。