24

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.

2
  • 3
    Unfortunately, JavaScript supports neither possessive quantifiers (that's the construct you're seeing here) nor atomic groups which are an alternate way of achieving the same result. Also, it doesn't support lookbehind assertions which this regex is also using. Long story short, you can't translate the regex into Java Script as it is. Jun 12, 2013 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. Jun 12, 2013 at 11:34

1 Answer 1

28

++

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.