正则表达式是什么,/(?!^)/ g是什么意思?

时间:2022-04-22 15:45:57

What does the regular expression, /(?!^)/g mean?

正则表达式是什么,/(?!^)/ g是什么意思?

I can see that from here x(?!y): Matches x only if x is not followed by y.

我可以从这里看到x(?!y):仅当x后面没有y时才匹配x。

That explains, if ?! is used before any set of characters, it checks for the not followed by condition.

这解释了,如果?!在任何字符集之前使用它,它检查未跟随条件。

But we have, ?!^. So, if we try to say it in words, it would probably mean not followed by negate. That really does not make me guess a probable statement for it. I mean negate of what?

但我们有,?!^。所以,如果我们试着用文字说出来,那么可能意味着不会出现否定。这真的不能让我猜测它是一个可能的陈述。我的意思是否定什么?

I'm still guessing and could not reach a fruitful conclusion.

我仍在猜测,无法得出有成效的结论。

Help is much appreciated.

非常感谢帮助。

Thanks!

2 个解决方案

#1


2  

Circumflex ^ only implies negation in a character class [^...] (when comes as first character in class). Outside of it it means start of input string or line. A negative lookahead that contains ^ only, means match shouldn't be found at start of input string or line.

Circumflex ^仅暗示字符类中的否定[^ ...](当作为类中的第一个字符出现时)。在它之外它意味着输入字符串或行的开始。只包含^的负前瞻,意味着在输入字符串或行的开头不应找到匹配。

See it in action

看到它在行动

#2


2  

It returns

  • all matches... (/g, global flag)
  • 所有比赛...(/ g,全球国旗)

  • ...which are not followed by... (x?!y, negative lookahead where x is the empty string)
  • ......后面没有......(x?!y,负前瞻,其中x是空字符串)

  • ...the position at the start of the string (^)
  • ...字符串开头的位置(^)

That is, any position between two characters except for the position at the start of the string, as you can see here.

也就是说,除了字符串开头的位置之外,两个字符之间的任何位置,如此处所示。

This regex is useful, thus, for detecting empty strings (after all, applying the regex to an empty string will return no matches). Empty strings may be the result of splitting strings, and you probably don't want to do anything with them.

因此,这个正则表达式对于检测空字符串很有用(毕竟,将正则表达式应用于空字符串将不返回任何匹配项)。空字符串可能是拆分字符串的结果,您可能不希望对它们执行任何操作。

#1


2  

Circumflex ^ only implies negation in a character class [^...] (when comes as first character in class). Outside of it it means start of input string or line. A negative lookahead that contains ^ only, means match shouldn't be found at start of input string or line.

Circumflex ^仅暗示字符类中的否定[^ ...](当作为类中的第一个字符出现时)。在它之外它意味着输入字符串或行的开始。只包含^的负前瞻,意味着在输入字符串或行的开头不应找到匹配。

See it in action

看到它在行动

#2


2  

It returns

  • all matches... (/g, global flag)
  • 所有比赛...(/ g,全球国旗)

  • ...which are not followed by... (x?!y, negative lookahead where x is the empty string)
  • ......后面没有......(x?!y,负前瞻,其中x是空字符串)

  • ...the position at the start of the string (^)
  • ...字符串开头的位置(^)

That is, any position between two characters except for the position at the start of the string, as you can see here.

也就是说,除了字符串开头的位置之外,两个字符之间的任何位置,如此处所示。

This regex is useful, thus, for detecting empty strings (after all, applying the regex to an empty string will return no matches). Empty strings may be the result of splitting strings, and you probably don't want to do anything with them.

因此,这个正则表达式对于检测空字符串很有用(毕竟,将正则表达式应用于空字符串将不返回任何匹配项)。空字符串可能是拆分字符串的结果,您可能不希望对它们执行任何操作。