[removed] Invalid quantifier in regex

前端 未结 2 1740
孤独总比滥情好
孤独总比滥情好 2020-12-19 04:17

The regex is constructed on the fly, but I\'ve output it to firebug:

(.{1,38})(+|$\\n?)

the error is

invalid quantifier +|$         


        
2条回答
  •  难免孤独
    2020-12-19 04:58

    A quantifier means "how many". The most common is "*" which means zero or more. The quantifier "+" means one or more.

    When you get an error about an illegal quantifier it almost always means you have a quantifier where it doesn't belong. For example, since they mean "how many" they must obviously refer to something. If you place one at the start of a pattern or group the regex is thinking "how many _of what?

    In your specific case you have a "+" immediately after the grouping character "(" which is why you get the error. You need to either escape the "+" so it isn't treated as a quantifier or put some character or group you want to match in front of it. In your case it is probably the first if you are trying to match an actual "+" character.

提交回复
热议问题