2012-06-15 64 views
0

我有不同的正则表达式匹配输出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". 

我不明白为什么会出现两个不同的输出和我缺少什么?

回答

0

\n关于Rubular被视为字符串(因为您正在输入文本视图)。但在真实的开发环境中,在一个字符串中,\n是逃脱了新行字符。所以"---\n- nicidnut\n"实际上是:

--- 
- nicidnut 

在irb中看到。

+0

非常感谢。它帮助:) – shailesh