调用String.replace时使用替换函数,我们能够检索匹配子字符串的偏移量。
var a = [];
"hello world".replace(/l/g, function (m, i) { a.push(i); });
// a = [2, 3, 9]
在上面的示例中,我们获得了匹配 l
个字符的偏移量列表。
我能否指望实现始终按出现的升序调用匹配函数,即使使用从右到左书写的语言?
也就是说:我能确定上面的结果总是 [2,3,9]
而不是 [3,9,2]
或任何其他这些偏移量的排列?
这是对 this question 的跟进那Tomalak回答:
Absolutely, yes. Matches are handled from left to right in the source string because left-to-right is how regular expression engines work their way to a string.
然而,关于 RTL 语言的情况,他还说:
That's a good question [...] RTL text definitely changes how JavaScript regular expressions behave.
我已经在 Chrome 中使用以下 RTL 片段进行了测试:
var a = [];
"بلوچی مکرانی".replace(/ی/g, function (m, i) { a.push(i); });
// a = [4, 11]
我不会说那种语言,但在查看字符串时,我看到
字符是字符串的第一个字符,也是空格之后的第一个字符。但是,由于文本是从右到左书写的,因此这些位置实际上是 空格之前的最后一个字符 和 字符串中的最后一个字符 - 转换为 [4,11]
因此,这似乎在 Chrome 中按预期工作。问题是:我可以相信所有兼容的 javascript 实现的结果都是一样的吗?
最佳答案
我用关键字“格式控制”、“从右到左”和“RTL”搜索了 ECMA-262 5.1 版/2011 年 6 月,但没有提及它们,除了它说格式控制字符是允许出现在字符串字面量和正则表达式字面量中。
摘自7.1节
It is useful to allow format-control characters in source text to facilitate editing and display. All format control characters may be used within comments, and within string literals and regular expression literals.
附件E
7.1: Unicode format control characters are no longer stripped from ECMAScript source text before processing. In Edition 5, if such a character appears in a
StringLiteral
orRegularExpressionLiteral
the character will be incorporated into the literal where in Edition 3 the character would not be incorporated into the literal
据此,我得出结论,JavaScript 对从右到左的字符的操作没有任何不同。它只知道存储在字符串中的 UTF-16 代码单元,并基于 logical order 工作。 .
关于javascript - 与 RTL 语言一起使用时字符串替换函数调用的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27905376/