21

I am porting some libraries from PHP to JavaScript and I came across this regular expression, some parts of it are unclear to me.

#(?: *+(?<= |^)\.((?:\([^)\n]++\)|\[[^\]\n]++\]|\{[^}\n]++\}|<>|>|=|<){1,4}?))#

Unclear parts are

  1. *+
  2. ++

I know, that this expression should accept strings like

.(title)[class]{style}<>
.[class]{style}<>
.[class](title){style}
// and so one - no metter of order \(.+\), \[.+\] and \{.+\} parts 
// and optional <>, >, = or < at the end

This expression is used with PCRE_UNGREEDY modifier.

Yatin
  • 2,348
  • 6
  • 20
  • 38
Jakub Truneček
  • 7,622
  • 2
  • 16
  • 32
  • 3
    Unfortunately, JavaScript supports neither possessive quantifiers (that's the construct you're seeing here) nor [atomic groups](http://www.regular-expressions.info/atomic.html) which are an alternate way of achieving the same result. Also, it doesn't support [lookbehind assertions](http://www.regular-expressions.info/lookaround.html) which this regex is also using. Long story short, you can't translate the regex into Java Script as it is. – Tim Pietzcker Jun 12 '13 at 11:28
  • @TimPietzcker That is sad, but to be honest, I counted on it. To be able to transform it, I needed to understand it first. – Jakub Truneček Jun 12 '13 at 11:34

1 Answers1

23

++

From What is double plus in regular expressions?

That's a Possessive Quantifier.

It basically means that if the regex engine fails matching later, it will not go back and try to undo the matches it made here. In most cases, it allows the engine to fail much faster, and can give you some control where you need it - which is very rare for most uses.

*+

*+ is the possessive quantifier for the * quantifier.

Community
  • 1
  • 1
Wouter J
  • 39,482
  • 15
  • 97
  • 108
  • 2
    This answer has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Quantifiers" – aliteralmind Apr 10 '14 at 00:12