admin管理员组文章数量:1424977
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)
wherey
is aregex
. 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
2 Answers
Reset to default 2Circumflex ^
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 wherex
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.
本文标签: javascriptWhat does the regular expression()g meanStack Overflow
版权声明:本文标题:javascript - What does the regular expression, (?!^)g mean? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745408488a2657357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论