5

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!

revo
  • 47,783
  • 14
  • 74
  • 117
devGeek
  • 213
  • 3
  • 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 May 24 '18 at 08: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 May 24 '18 at 08:57
  • 1
    *"I'm still guessing"* -- stop guessing, start reading the documentation. The JavaScript documentation is scarce, indeed. I recommend you the [PHP documentation of regular expressions](http://php.net/manual/en/reference.pcre.pattern.syntax.php). It is more detailed. – axiac May 24 '18 at 08:59
  • 2
    Sites such as regex101.com have a built-in explanation component that parses and explains the pattern you specify. – Fred May 24 '18 at 09: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 May 24 '18 at 09:03
  • 1
    I suggest you to use [RegExr](https://regexr.com/) to learn about regex – attempt0 May 24 '18 at 13:08
  • This shows the expression matches infinitely – attempt0 May 24 '18 at 13:10

2 Answers2

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.

See it in action

falinsky
  • 7,229
  • 3
  • 32
  • 56
revo
  • 47,783
  • 14
  • 74
  • 117
  • You assume that `/`and `/g`is not part of the pattern? I assume it's part of the pattern and I fail to see what the difference is between `//g` and `/(?!^)/g` – Lieven Keersmaekers May 24 '18 at 09:01
  • @LievenKeersmaekers I assume them as part of pattern but delimiters are constant and we don't need flags here to explain the logic. So I could skip them easily. The difference is `//g` matches positions and this `/(?!^)/g` does the same with one match less. – revo May 24 '18 at 09:10
  • I don't think it does?! Using Regexbuddy's Debugging facility, `/(?!^)/g` needs 5 steps where `//g` only takes 3 steps to find a match. – Lieven Keersmaekers May 24 '18 at 09:15
  • 1
    @LievenKeersmaekers RegexBuddy doesn't accept delimiters and flags as in JS. You don't put `/(?!^)/g` but `(?!^)`. – revo May 24 '18 at 09:23
  • 1
    tx, that was what tripped me up. The regex is `(?!^)`. The rest is javascript fluff. – Lieven Keersmaekers May 24 '18 at 09:26
2

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.

dgstranz
  • 135
  • 8