最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Differences between Ruby 1.9 and Javascript regexp - Stack Overflow

programmeradmin4浏览0评论

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?

I'm looking for subtle differences where the same regex might behave differently, for example /^abc$/ will match this in Ruby:

123
abc
def

But it won't match in Javascript.

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?

I'm looking for subtle differences where the same regex might behave differently, for example /^abc$/ will match this in Ruby:

123
abc
def

But it won't match in Javascript.

Share Improve this question edited Jan 9, 2012 at 4:23 asked Jan 9, 2012 at 3:49 user47322user47322 2
  • 1 There's also differences between particular versions of Ruby. Ruby 1.8 does not support look-behind assertions, while Ruby 1.9 does. – tybro0103 Commented Jan 9, 2012 at 3:53
  • @tybro0103 Oops, should've specified which version. – user47322 Commented Jan 9, 2012 at 4:24
Add a ment  | 

1 Answer 1

Reset to default 18

Features supported by Ruby, but not JavaScript:

  • \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)
  • (?#ment)
  • Free-spacing syntax supported
  • Character class is a single token
  • # starts a ment
  • [: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)

Features supported by JavaScript, but not Ruby:

  • \cA through \cZ (control character)
  • \ca through \cz (control character)
  • \u0000 through \uFFFF (Unicode character)

Source:

  • http://www.regular-expressions.info/refflavors.html
发布评论

评论列表(0)

  1. 暂无评论