首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JavaScript中检测卷曲/ MS智能引号

在JavaScript中检测卷曲/ MS智能引号
EN

Stack Overflow用户
提问于 2016-04-27 02:41:54
回答 1查看 576关注 0票数 1

我正在做一些用户输入验证(主要是法语字符)。首先,我尝试检测输入到文本框中的字符。但是,我在尝试检测MS智能引号(花式引号)时遇到问题。代码无法识别智能引号。我不确定是否可能当我测试时,如果我实际上没有输入智能引号,或者是正则表达式有问题。MS智能报价代码是最后2个js条件。

代码语言:javascript
复制
   <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>
EN

回答 1

Stack Overflow用户

发布于 2016-04-27 04:27:20

最后两个else if块有一些问题。首先,检查智能双引号的那个不必要地包装在[...]中。如果需要检查字符串是否与模式匹配,请使用RegExp#test() (不带全局修饰符)。

因此,我建议使用

代码语言:javascript
复制
//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 ');
  }

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36873236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档