0

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

1) 红色

/\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".

我不明白为什么有两个不同的输出,我错过了什么?

4

1 回答 1

0

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

---
- nicidnut

如 irb 中所见。

于 2012-06-15T14:43:06.257 回答