What does this regular expression mean?

/\n\s*\n/s

I think it means

  • new line
  • followed by 0 or more spaces
  • then a new line

It is right? If not, can you explain?

+3
source share
3 answers

Almost right: \sin fact, means "any space character", which also includes tabs and possibly, CR / LF ( \rand \n).

If you use perl, it \smay also match some other characters (from perlre ):

If Unicode is valid, "\ s" also matches "\ x {85}", "\ x {2028} and" \ x {2029} "

A few more clarifications.

  • (^ $ ), , ANYWHERE . , /cat/ "cat", "caterpillar" "concatenate". , , , .
  • /s , . , perlre:

. , "." , , .

"/s" "/m" $*. , , $*, "/s" "/m" "^" "$", ( ) . , /ms, "." , "^" "$" , .

+8

. \s - , .

+1

.

-, , \s , "".

In addition, trailing smeans that the regex is in "single line mode", which means that .(dot) will match newlines. In other words, everything you pass to this regular expression will be parsed as if it were a single line of input, regardless of whether it has line breaks (they will be displayed as another special character, for example, \nor \r).

+1
source

All Articles