Regex输出中的异常

时间:2012-06-15 14:31:44

标签: regex irb rubular

我在正则表达式匹配时有不同的输出1)当使用Rubular和2)其他使用rails console或irb环境时。

1)Rubular

/\w+/ on test string "---\n- nicidnut\n" matches "    n  nicidnut n" 
(I have shown only matched part and put spaces for unmatched string characters)

2)irb

(/\w+/).match("---\n- nicidnut\n") produces "nicidnut".

我无法理解为什么有两种不同的输出,我缺少什么?

1 个答案:

答案 0 :(得分:0)

Rubular上的

\n被视为文字字符串(因为您在文本视图中输入)。但在实际开发环境中,在一个字符串中,\n是转义换行符。所以"---\n- nicidnut\n"实际上是:

---
- nicidnut

见于irb。