views:

95

answers:

2

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

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

the error is

invalid quantifier +|$\n?)

I don't even know what invalid quantifier means! I really feel like I'm going to get the "Show us something relevant" answer, but I'm not sure where to start.

The actual code is:

var re = top.RegExp;
var regex = new re("(.{1," + len + "})(+|$\\n?)", "gm");

UPDATE: Per Bennor McCarthy's instructions, I changed the code to this:

 var regex = new re("(.{1," + len + "})(\+|\$\\n?)", "gm");

Firebug still tells me this:

invalid quantifier +|$\n?)
[Break on this error] var regex = new re("(.{1," + len + "})(\+|\$\\n?)", "gm"); 

ANOTHER UPDATE Looks Like I had to double slash it and this solved the problem!

final code

var regex = new re("(.{1," + len + "})(\\+|\\$\\n?)", "gm");
+7  A: 

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.

Bennor McCarthy
need to escape the $ too, if it isn't meant to match the end of the line, which it doesn't seem to as he has the (maybe redundant) '\n'. if he wants it to match the actual chars $\n, it's best to use \$$
Luke Schafer
Based on the code he's added, it looks like he's trying to match the end of the line and consume the newline character (using the multi-line modifier)
Bennor McCarthy
So, based on my final code above, is the slash in front of the $ needed? I see you took it out of your example.
Stephen
It depends on what you're trying to match, if it's the end of the line, then you don't need to escape it. If you're trying to match the literal character '$', then you do. Because you're writing your regular expression inside a string, you have to double up on the backslashes because the backslash has a special meaning inside a string, i.e. You need one backslash to escape the special character in the regex, and one to escape the slash in the string.
Bennor McCarthy
+1  A: 

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.

Bryan Oakley
Thank You for the answer!
Stephen