Ok, you can use + after * . You can read a little about it on this site . + after * is called possessive quantifier.
What is he doing? This prevents return * .
Usually, when you have something like .*c and using this to match abcde .* Will first match the entire string ( abcde ), and since the regular expression cannot match c after .* , The engine will return one character at a time to check if there is a match (this is a rollback).
As soon as it returns to c , you will get an abc match with abcde .
Now imagine that the engine should cancel several hundred characters, and if you have nested groups and several * (or + or {m,n} ) forms, you can quickly get thousands, millions of characters to return, called catastrophic rollback .
Possessive quantifiers are used here. They actually impede any form of retreat. In the above regex, abcde will not match .*+c abcde As soon as .*+ Consumes the entire line, it cannot back out, and since there is no c at the end of the line, the match is not performed.
So another possible use of possessive quantifiers is that they can improve the performance of some regular expressions if the engine can support it.
For your regular expression /^[\W].*+$/ , I donβt think there is an improvement (perhaps a slight improvement) that the possessive quantifier gives. Lastly, it can easily be rewritten as /^\W.*+$/ .
Jerry source share