dencey
dencey

Reputation: 1061

What does this RegExp pattern /(?:^|:|,)(?:\s*\[)+/g mean in Javascript?

In Jquery there is a regexp patten definition

var rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g;

this pattern matches strings like "abc,[" and "abc:[", but not for "abc^[". So what's the meaning of this part in the pattern:

(?:^|:|,)

Upvotes: 0

Views: 995

Answers (3)

Johnny
Johnny

Reputation: 7321

(?:^|:|,) means match ^ or : or ,. Ordinarily this would also capture these characters because of the brackets but because of the ?: modifier they won't be caught.

Update: whoops, true enough. ^ matches beginning of string in this context, not the symbol itself.

Upvotes: 1

porges
porges

Reputation: 30580

(?: ... ) is a group (like (...)) that doesn't capture anything.

So your example (?:^|:|,) simply matches either the start of the text, a colon, or a comma.

this pattern matches strings like "abc,[" and "abc:[", but not for "abc^[".

It sounds like you don't know what ^ means - in a regex, it means "the start of the string" (unless you've turned on multi-line mode, where it means "the start of the line").

Upvotes: 1

Wouter J
Wouter J

Reputation: 41934

  • () means a capturing group
  • ?: if you place this in the front of a group it won't be captured, so the capturing group become only a group of characters.
  • ^|:|, means it matches the begin of the line (^), or a : or a ,. The | is the seperator between these tokens.

Upvotes: 1

Related Questions