简体   繁体   English

当在字符序列的末尾添加空格时,为什么此RegEx失败?

[英]Why does this RegEx fail when a space is added to end of character sequence?

Here's some code that I've got in my javascript to detect whether or not a string is a Right-To-Left (RTL) script: 这是我的JavaScript中提供的一些代码,用于检测字符串是否是从右向左(RTL)脚本:

is_right_to_left : function (text) {

      /*
       * Right-to-left Unicode blocks for modern scripts are:
       *
       * Consecutive range of the main letters:
       * U+0590  to U+05FF  - Hebrew
       * U+0600  to U+06FF  - Arabic
       * U+0700  to U+074F  - Syriac
       * U+0750  to U+077F  - Arabic Supplement
       * U+0780  to U+07BF  - Thaana
       * U+07C0  to U+07FF  - N'Ko
       * U+0800  to U+083F  - Samaritan
       *
       * Arabic Extended:
       * U+08A0  to U+08FF  - Arabic Extended-A
       *
       * Consecutive presentation forms:
       * U+FB1D  to U+FB4F  - Hebrew presentation forms
       * U+FB50  to U+FDFF  - Arabic presentation forms A
       *
       * More Arabic presentation forms:
       * U+FE70  to U+FEFF  - Arabic presentation forms B
       */

        var ltrChars        = 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF'+'\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF',
            rtlChars        = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC',
            rtlDirCheck     = new RegExp('^[^'+ltrChars+']*['+rtlChars+']');

        return rtlDirCheck.test(text);

    }

This is working up to all the tests I've done. 这正在完成我所做的所有测试。 However, if I add a space to some sequences of characters from an RTL script, it fails the test. 但是,如果我在RTL脚本的某些字符序列中添加空格,则测试将失败。 For example, if I have ﺮﺳﻷﺍ the function correctly detects that the string is RTL. 例如,如果我有ﺮﺳﻷﺍ函数将正确检测到字符串为RTL。 However, if I add a trailing space then the function reports that it is NOT an RTL script. 但是,如果添加尾随空格,则该函数将报告它不是RTL脚本。 What is wrong with my RegEx? 我的RegEx有什么问题? I want to be sure that a space won't throw it off. 我想确保不会有空格。 Any ideas? 有任何想法吗?

I don't want a space anywhere to throw it off, not just at the end. 我不希望任何地方都有空间,而不仅仅是在最后。 How can I achieve that? 我该如何实现?

You want to use a negative look ahead operator. 您想使用否定的前瞻运算符。

 function is_right_to_left (text) { /* * Right-to-left Unicode blocks for modern scripts are: * * Consecutive range of the main letters: * U+0590 to U+05FF - Hebrew * U+0600 to U+06FF - Arabic * U+0700 to U+074F - Syriac * U+0750 to U+077F - Arabic Supplement * U+0780 to U+07BF - Thaana * U+07C0 to U+07FF - N'Ko * U+0800 to U+083F - Samaritan * * Arabic Extended: * U+08A0 to U+08FF - Arabic Extended-A * * Consecutive presentation forms: * U+FB1D to U+FB4F - Hebrew presentation forms * U+FB50 to U+FDFF - Arabic presentation forms A * * More Arabic presentation forms: * U+FE70 to U+FEFF - Arabic presentation forms B */ var ltrChars = 'A-Za-z\\\À-\\\Ö\\\Ø-\\\ö\\\ø-\\\ʸ\\\̀-\\\֐\\\ࠀ-\\\῿'+'\\\Ⰰ-\\\﬜\\\﷾-\\\﹯\\\﻽-\\\￿', rtlChars = '\\\֑-\\\߿\\\יִ-\\\﷽\\\ﹰ-\\\ﻼ', rtlDirCheck = new RegExp('^(?!.*['+ltrChars+'\\\\s]+.*)['+rtlChars+']$'); return rtlDirCheck.test(text); } alert(is_right_to_left("ﺮﺳﻷ ﺍ")); 

Here is a quick description of the regex: 这是正则表达式的简要说明:

正则表达式可视化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式验证 url 并且不以分号或空格结尾 - Regex to validate url AND does not end in semicolon or space 为什么在年末字符串末尾添加空格字符会导致JavaScript(节点)应用时区偏移量? - Why does adding a space character at the end of a year string cause JavaScript (node) to apply the timezone offset? 当我在RegEx中转义<字符时,为什么jslint会抱怨? - Why does jslint complain when I escape the < character in a RegEx? 正则表达式捕获最后一个空格字符和字符串结尾之间的字符 - Regex to capture characters between the last white space character and the end of string 在正则表达式中查找字符或序列 - Looking for a character OR a sequence in regex 仅当不存在其他字符时,RegEx才匹配结束字符 - RegEx match end character only when other character is not present 正则表达式为什么要在其匹配项中包含空格 - Why does Regex Include the space in its Match 正则表达式字符替换为空格 - regex character replacement with space 为什么在使用逗号运算符添加值时,console.log会在句子末尾添加空格? - Why does console.log add a space at the end of my sentence, when adding a value with the comma operator? 当从数据列表中选择项目时,为什么值末尾的空格会消失? - Why does a space at the end of a value disappear when selecting an item from a datalist?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM