如何在输入字段中检测非"GSM 7位字母"字符

Geo*_* D. 6 javascript regex gsm

我试图检测文本输入字段是否有任何不属于GSM 7位字母表的字符.带有字符的表格在这里http://www.dreamfabric.com/sms/default_alphabet.html

经过大量的搜索,我发现这个(我需要检查一些非拉丁字符需要什么正则表达式?)它非常接近我想要完成的事情因为它检测到非拉丁字符.如何更改正则表达式以包含GSM 7位字母表?

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>test foreign chars</title>
</head>
<body>

    <input id="foreign_characters" size="12" type="text" name="foreign_characters" value="test">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">

(function(){

    $('#foreign_characters').on("keyup", function(){

        var foreignCharacters = $("#foreign_characters").val();
        var rforeign = /[^\u0000-\u007f]/;

        if (rforeign.test(foreignCharacters)) {
          alert("This is non-Latin Characters");
        } else {
          alert("This is Latin Characters");
        }

    });

})();

    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Laj*_*pad 17

function isGSMAlphabet(text) {
    var regexp = new RegExp("^[A-Za-z0-9 \\r\\n@£$¥èéùìòÇØøÅå\u0394_\u03A6\u0393\u039B\u03A9\u03A0\u03A8\u03A3\u0398\u039EÆæßÉ!\"#$%&'()*+,\\-./:;<=>?¡ÄÖÑܧ¿äöñüà^{}\\\\\\[~\\]|\u20AC]*$");

    return regexp.test(text);
}
Run Code Online (Sandbox Code Playgroud)

这个正则表达式可以解决您的问题.

  • 正则表达式没问题,但你错了.使用`var rforeign =/regex /;`或`var rforeign = new RegExp("regex");`. (3认同)
  • 正则表达式缺少字符 \u00A4 (2认同)

Sjo*_*erd 12

您可以将所有有效字符放在字符串中,然后重复搜索字符串.

gsm = "@£$¥èéùìòÇØøÅå?_?????????^{}\[~]|€ÆæßÉ!\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà";
var letter = 'a';
var letterInAlfabet = gsm.indexOf(letter) !== -1;
Run Code Online (Sandbox Code Playgroud)

如果你使用它,请确保你的编码正确,即将你的Javascript文件保存为UTF8并指定它是浏览器的UTF8.


Tom*_*cer 9

接受的答案是可行的,但它们会受到复杂性(使用正则表达式)和性能(需要搜索两个数组)的影响。这是一个性能更好的解决方案,因为使用了查找集,以及如果找到非 GSM7 字符就会短路的循环。使用 Unicode 点,以便在剪切和粘贴此代码时,不同的字符编码不会出现问题。

\n
const gsmCodePoints = new Set([\n  0x000a, 0x000c, 0x000d, \n  0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,\n  0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,\n  0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d,\n  0x004e, 0x004f,\n  0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059,  0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, \n  0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f, \n  0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e,\n  0x00a1, 0x00a3, 0x00a4, 0x00a5, 0x00a7,\n  0x00bf,\n  0x00c4, 0x00c5, 0x00c6, 0x00c7, 0x00c9,\n  0x00d1, 0x00d6, 0x00d8, 0x00dc, 0x00df,\n  0x00e0, 0x00e4, 0x00e5, 0x00e6, 0x00e8, 0x00e9, 0x00ec,\n  0x00f1, 0x00f2, 0x00f6, 0x00f8, 0x00f9, 0x00fc,\n  0x0393, 0x0394, 0x0398, 0x039b, 0x039e, 0x03a0, 0x03a3, 0x03a6, 0x03a8, 0x03a9,\n  0x20ac,\n]);\n\nfunction isGsmMessage(message) {\n  for (const s of message) {\n    const codePoint = s.codePointAt(0);\n    if (codePoint && !gsmCodePoints.has(codePoint)) {\n      return false;\n    }\n  }\n  return true;\n}\n\nisGsmMessage(\'foo\'); // -> true\nisGsmMessage(\'\xe2\x9a\xa1\xef\xb8\x8f bar \'); // -> false\n\n// All GSM characters\nisGsmMessage(\'@\xc2\xa3$\xc2\xa5\xc3\xa8\xc3\xa9\xc3\xb9\xc3\xac\xc3\xb2\xc3\x87\\n\xc3\x98\xc3\xb8\\r\xc3\x85\xc3\xa5\xce\x94_\xce\xa6\xce\x93\xce\x9b\xce\xa9\xce\xa0\xce\xa8\xce\xa3\xce\x98\xce\x9e\xc3\x86\xc3\xa6\xc3\x9f\xc3\x89\\x20!"#\xc2\xa4%&\\\'()*+,-./0123456789:;<=>?\xc2\xa1ABCDEFGHIJKLMNOPQRSTUVWXYZ\xc3\x84\xc3\x96\xc3\x91\xc3\x9c\xc2\xa7\xc2\xbfabcdefghijklmnopqrstuvwxyz\xc3\xa4\xc3\xb6\xc3\xb1\xc3\xbc\xc3\xa0\\f^{}\\\\[~]|\xe2\x82\xac\'); // -> true\n
Run Code Online (Sandbox Code Playgroud)\n