Search code examples
regexirbrubular

Anomaly in Regex output


I am having different outputs on regex matching 1) when using Rubular and 2) other when using rails console or irb environment.

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

I can't understand why there is two different outputs and what am I missing?


Solution

  • \n on Rubular is treated as literal string (since you are entering in text view). But in real development environment, in a string, \n is escaped new line character. So "---\n- nicidnut\n" is actually:

    ---
    - nicidnut
    

    as seen in irb.