문제

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?

도움이 되었습니까?

해결책

Just as a slightly longer explanation: \S is a metacharacter which is interpreted as /[^ \t\r\n\f]/, whereas \s is a metacharacter which is interpreted as /[ \t\r\n\f]/ -> 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.

다른 팁

\S is just negated with \s, so it is the same with [^\s], there are no difference.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top