rubyregexslim-lang

Is there any difference between \S and [^\s] in Ruby Regex statements?


In the code for Slim's parser, there is the following regex statement:

/\A(?:#{keys}|\*(?=[^\s]+)|(#{WORD_RE}(?:#{WORD_RE}|:|-)*#{WORD_RE}|#{WORD_RE}+))/

Could \S be substituted for the [^\s]?

Would the statement behave any differently?


Solution

  • Just as a slightly longer explanation:

    (see documentation)

    So the interpretation of the characters is exactly opposite, which means if you are inverting one of the two, you'll get exactly the same matching behaviour.

    And that means yes, you can substitute [^\s] in your regular expression with \S.