我正在做一些用户输入验证(主要是法语字符)。首先,我尝试检测输入到文本框中的字符。但是,我在尝试检测MS智能引号(花式引号)时遇到问题。代码无法识别智能引号。我不确定是否可能当我测试时,如果我实际上没有输入智能引号,或者是正则表达式有问题。MS智能报价代码是最后2个js条件。
<body>
<script>
function addComment() {
var comm = document.getElementById("comment").value;
if (!(comm.indexOf('\u0152') === -1))
{
alert('found OE ligature');
}
else if (!(comm.indexOf('\u0153') === -1))
{
alert('found oe ligature');
}
else if (!(comm.indexOf('\xAB') === -1))
{
alert('found <<');
}
else if (!(comm.indexOf('\xBB') === -1))
{
alert('found >>');
}
else if ((comm.match([/[\u201C\u201D\u201E]/g]) ))
{
alert('found MS Smart double quotes and apostrophe ');
}
else if ((comm.match(/[\u2018\u2019\u201A]/g) ))
{
alert('found MS Smart single quotes and apostrophe ');
}
}
</script>
<input type="text" id="comment" onblur="addComment()">
</body>发布于 2016-04-27 04:27:20
最后两个else if块有一些问题。首先,检查智能双引号的那个不必要地包装在[...]中。如果需要检查字符串是否与模式匹配,请使用RegExp#test() (不带全局修饰符)。
因此,我建议使用
//var comm = "‘ “ ” ";
var comm = "‘ ";
if (!(comm.indexOf('\u0152') === -1)) {
alert('found OE ligature');
} else if (!(comm.indexOf('\u0153') === -1)) {
alert('found oe ligature');
} else if (!(comm.indexOf('\xAB') === -1)) {
alert('found <<');
} else if (!(comm.indexOf('\xBB') === -1)) {
alert('found >>');
} else if (/[\u201C\u201D\u201E]/.test(comm)) {
alert('found MS Smart double quotes and apostrophe ');
} else if (/[\u2018\u2019\u201A]/.test(comm)) {
alert('found MS Smart single quotes and apostrophe ');
}
https://stackoverflow.com/questions/36873236
复制相似问题