这些regex之间有什么区别

时间:2023-01-24 00:06:54

I'm reading Ionic's source code. I came across this regex, and i"m pretty baffled by it.

我正在读Ionic的源代码。我遇到了这个雷格斯,我很困惑。

([\s\S]+?)

Ok, it's grouping on every char that is either a white space, or non white space???

它对每一个字符进行分组这是一个空格,还是非空格???

Why didn't they just do

他们为什么不这么做呢

(.+?)

Am I missing something?

我遗漏了什么东西?

5 个解决方案

#1


11  

The . matches any symbol but a newline. In order to make it match a newline, in most languages there is a modifier (dotall, singleline). However, in JS, there is no such a modifier.

的。匹配除换行之外的任何符号。为了使它与换行符匹配,在大多数语言中有一个修饰符(dotall, singleline)。但是,在JS中没有这样的修饰符。

Thus, a work-around is to use a [\s\S] character class that will match any character, including a newline, because \s will match all whitespace and \S will match all non-whitespace characters. Similarly, one could use [\d\D] or [\w\W].

因此,解决方法是使用一个[\s\ s]字符类来匹配任何字符,包括换行,因为\s将匹配所有空格,而\s将匹配所有非空格字符。同样,你也可以使用[\d\ d]或[\w\ w\ w]。

Also, there is a [^] pattern to match the same thing in JS, but since it is JavaScript-specific, the regexes containing this pattern are not portable between regex flavors.

同样,有一个[^]模式匹配JS中的同样的事情,但因为它是JavaScript-specific,regex包含正则表达式之间的这种模式是不可移植的味道。

The +? lazy quanitifier matches 1 or more symbols conforming to the preceding subpattern, but as few as possible. Thus, it will match just 1 symbol if used like this, at the end of the pattern.

+ ?懒惰的quanitifier匹配1个或多个符合上述子模式的符号,但要尽可能少。因此,如果像这样使用,它将只匹配一个符号,在模式的末尾。

#2


3  

In many realizations of Regexp "." doesn't match new lines. So they use "[\s\S]" as a little hack =)

在Regexp的许多实现中“.”与新行不匹配。所以他们使用"[\s\ s\ s]"作为一个小技巧=)

#3


3  

A . matches everything but the newline character. This is actually a well known/documented problem with javascript. The \s (whitespace match) alongside it's negation \S (non-whitespace match) provides a dotall match including the newline. Thus [\s\S] is generally used more frequently than .

一个。匹配除换行符以外的所有内容。这实际上是javascript一个众所周知的/文档化的问题。(空格匹配)和它的否定\s(非空格匹配)提供了一个dotall匹配,包括换行符。因此,通常使用的频率比。

#4


1  

The RegEx they used includes more characters (essentially everything).

他们使用的RegEx包含了更多的字符(基本上是所有的字符)。

\s matches any word or digit character or whitespace.

\s匹配任何单词或数字字符或空格。

\S matches anything except a digit, word character, or whitespace

\S匹配除数字、字元或空格外的任何东西

As Casimir notes:

卡西米尔指出:

. matches any character except newline (\n)

。匹配除换行外的任何字符(\n)

#5


1  

. matches any char except carriage return /r and new line /n

。匹配除回车/r和新行/n之外的任何字符

The Shortest way to do [/s/S](white space and non white space) is [^](not nothing)

最短的方法[/ s / s](空白和非空格)[^](不是没有)

#1


11  

The . matches any symbol but a newline. In order to make it match a newline, in most languages there is a modifier (dotall, singleline). However, in JS, there is no such a modifier.

的。匹配除换行之外的任何符号。为了使它与换行符匹配,在大多数语言中有一个修饰符(dotall, singleline)。但是,在JS中没有这样的修饰符。

Thus, a work-around is to use a [\s\S] character class that will match any character, including a newline, because \s will match all whitespace and \S will match all non-whitespace characters. Similarly, one could use [\d\D] or [\w\W].

因此,解决方法是使用一个[\s\ s]字符类来匹配任何字符,包括换行,因为\s将匹配所有空格,而\s将匹配所有非空格字符。同样,你也可以使用[\d\ d]或[\w\ w\ w]。

Also, there is a [^] pattern to match the same thing in JS, but since it is JavaScript-specific, the regexes containing this pattern are not portable between regex flavors.

同样,有一个[^]模式匹配JS中的同样的事情,但因为它是JavaScript-specific,regex包含正则表达式之间的这种模式是不可移植的味道。

The +? lazy quanitifier matches 1 or more symbols conforming to the preceding subpattern, but as few as possible. Thus, it will match just 1 symbol if used like this, at the end of the pattern.

+ ?懒惰的quanitifier匹配1个或多个符合上述子模式的符号,但要尽可能少。因此,如果像这样使用,它将只匹配一个符号,在模式的末尾。

#2


3  

In many realizations of Regexp "." doesn't match new lines. So they use "[\s\S]" as a little hack =)

在Regexp的许多实现中“.”与新行不匹配。所以他们使用"[\s\ s\ s]"作为一个小技巧=)

#3


3  

A . matches everything but the newline character. This is actually a well known/documented problem with javascript. The \s (whitespace match) alongside it's negation \S (non-whitespace match) provides a dotall match including the newline. Thus [\s\S] is generally used more frequently than .

一个。匹配除换行符以外的所有内容。这实际上是javascript一个众所周知的/文档化的问题。(空格匹配)和它的否定\s(非空格匹配)提供了一个dotall匹配,包括换行符。因此,通常使用的频率比。

#4


1  

The RegEx they used includes more characters (essentially everything).

他们使用的RegEx包含了更多的字符(基本上是所有的字符)。

\s matches any word or digit character or whitespace.

\s匹配任何单词或数字字符或空格。

\S matches anything except a digit, word character, or whitespace

\S匹配除数字、字元或空格外的任何东西

As Casimir notes:

卡西米尔指出:

. matches any character except newline (\n)

。匹配除换行外的任何字符(\n)

#5


1  

. matches any char except carriage return /r and new line /n

。匹配除回车/r和新行/n之外的任何字符

The Shortest way to do [/s/S](white space and non white space) is [^](not nothing)

最短的方法[/ s / s](空白和非空格)[^](不是没有)