Ruby 1.9和Javascript regexp之间的差异 [英] Differences between Ruby 1.9 and Javascript regexp
本文介绍了Ruby 1.9和Javascript regexp之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
除了Javascript的 ^
和 $
等同于Ruby的 \ A
和 \ z
,两个正则表达式引擎之间还存在哪些细微差别?
Apart from Javascript's ^
and $
being equivalent to Ruby's \A
and \z
, what other subtle differences are there between the two regular expression engines?
我正在寻找相同的正则表达式可能表现不同的细微差别,例如 / ^ abc $ /
将在Ruby中匹配:
I'm looking for subtle differences where the same regex might behave differently, for example /^abc$/
will match this in Ruby:
123
abc
def
但它在Javascript中不匹配。
But it won't match in Javascript.
推荐答案
Ruby支持的功能,但不支持JavaScript:
Features supported by Ruby, but not JavaScript:
-
\ a
(钟) -
\ e
(转义) -
\ A
(开始字符串) -
\ Z
(字符串结尾,最后换行符之前) -
\ z
(字符串结尾) - 转发引用
\1
通过\9
- 对fa的反向引用iled群组也失败
-
(?> regex)
(原子组) -
\ G
(比赛尝试开始) -
(?#comment)
- 支持自由间距语法
- 字符类是单个标记
-
#
发表评论 -
[:alpha:]
POSIX字符类 -
(?i)
(不区分大小写)(JavaScript仅支持/ i
) -
(?s)
(点匹配换行符)(?m) -
( ?m)
(^
和$
在换行符时匹配)(/ m
仅限JavaScript) -
(?x)
(自由间隔模式) -
(? - ismxn)
(关闭模式修饰符) -
(?ismxn:group)
(组的本地模式修饰符)
\a
(bell)\e
(escape)\A
(start of string)\Z
(end of string, before final line break)\z
(end of string)- Forward references
\1
through\9
- Backreferences to failed groups also fail
(?>regex)
(atomic group)\G
(start of match attempt)(?#comment)
- Free-spacing syntax supported
- Character class is a single token
#
starts a comment[:alpha:]
POSIX character class(?i)
(case insensitive) (JavaScript supports/i
only)(?s)
(dot matches newlines) (?m)(?m)
(^
and$
match at line breaks) (/m
only in JavaScript)(?x)
(free-spacing mode)(?-ismxn)
(turn off mode modifiers)(?ismxn:group)
(mode modifiers local to group)
JavaScript支持的功能,但是不是Ruby:
Features supported by JavaScript, but not Ruby:
-
\ cA
通过\ CZ
(控制角色) -
\ ca
通过\ cz
(控制字符) -
\ u0000
通过\\FFFF
(Unicode字符)
\cA
through\cZ
(control character)\ca
through\cz
(control character)\u0000
through\uFFFF
(Unicode character)
资料来源:
- http://www.regular-expressions.info/refflavors.html
这篇关于Ruby 1.9和Javascript regexp之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文