[英]Why does this RegEx fail when a space is added to end of character sequence?
这是我的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);
}
这正在完成我所做的所有测试。 但是,如果我在RTL脚本的某些字符序列中添加空格,则测试将失败。 例如,如果我有ﺮﺳﻷﺍ
函数将正确检测到字符串为RTL。 但是,如果添加尾随空格,则该函数将报告它不是RTL脚本。 我的RegEx有什么问题? 我想确保不会有空格。 有任何想法吗?
我不希望任何地方都有空间,而不仅仅是在最后。 我该如何实现?
您想使用否定的前瞻运算符。
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("ﺮﺳﻷ ﺍ"));
这是正则表达式的简要说明:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.