[英]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.