javascript - What does the regular expression, (?!^)g mean? - Stack Overflow

What does the regular expression, (?!^)g mean?I can see that from here x(?!y): Matches x only if x

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

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

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!

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

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

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!

Share Improve this question edited May 24, 2018 at 13:10 revo 48.8k15 gold badges83 silver badges122 bronze badges asked May 24, 2018 at 8:51 devGeekdevGeek 3033 silver badges9 bronze badges 7
  • 1 "That explains, if ?! is used before any set of characters" -- it is not ?!. It is (?!y) where y is a regex. The parentheses are part of the notation. Without them, ?! does not have any special meaning (apart from ? marking the piece it follows as being optional). – axiac Commented May 24, 2018 at 8:56
  • 1 "it would probably mean not followed by negate." -- ^ means "negate" only when it is the first character of a character set ([^...]). Otherwise it means beginning of string (or beginning of line). – axiac Commented May 24, 2018 at 8:57
  • 1 "I'm still guessing" -- stop guessing, start reading the documentation. The JavaScript documentation is scarce, indeed. I remend you the PHP documentation of regular expressions. It is more detailed. – axiac Commented May 24, 2018 at 8:59
  • 2 Sites such as regex101. have a built-in explanation ponent that parses and explains the pattern you specify. – Fred Commented May 24, 2018 at 9:01
  • 2 @Fred They have a breakdown but flow of matching couldn't be explained well by looking at some lines explaining a token. – revo Commented May 24, 2018 at 9:03
 |  Show 2 more ments

2 Answers 2

Reset to default 2

Circumflex ^ only implies negation in a character class [^...] (when es 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.

See it in action

It returns

  • all matches... (/g, global flag)
  • ...which are not followed by... (x?!y, negative lookahead where x is the empty string)
  • ...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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745408488a4626421.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信