What is the difference between [\ s \ S] *? and.*?

I met the following token in a regular expression: [\s\S]*?

If I understand this correctly, a character class means "match whitespace or no spaces." Therefore, it will not do the same as .*?

One possible difference is that usually . does not match newlines. However, this regular expression was written in Ruby and the m modifier was passed, which means that . really matches newlines.

Is there any other reason to use [\s\S]*? instead of .*?

In case this helps, the regular expression that I look up appears in the sprockets library in the constant HEADER_PATTERN on line 97. The full expression is:

 / \A \s* ( (\/\* ([\s\S]*?) \*\/) | (\#\#\# ([\s\S]*?) \#\#\#) | (\/\/ ([^\n]*) \n?)+ | (\# ([^\n]*) \n?)+ ) /mx 
+8
ruby regex
source share
3 answers

You correctly interpreted the regular expression.

This is similar to a relic from other languages ​​that do not support m-flag (or s-flag in other implementations).

The reason for using this construct would be to not use the m flag so that you have the opportunity to use. without newline matching, but can still match all if necessary.

+6
source share

They will be the same with the m flag, except that .* Will be much clearer and easier to maintain.

0
source share

The newline value is the only difference. Perhaps someone thought it was easier to read without knowing the context m, or would like it to be resistant to change in this context.

I saw [^]* used for a similar purpose.

0
source share

All Articles