JavaScript: Invalid quantifier in regex

拟墨画扇 提交于 2019-11-29 10:09:45

The problem is the +, which is a quantifier you need to escape.

Use this instead:

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

or inside a string:

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

If you want to match the literal $ followed by a newline, you need to escape the $ with \ (or \\ inside a string - see my last comment below this for an explanation).

Here's some information on quantifiers.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!