Error de cuantificador no válido de expresiones regulares de javascript

Tengo el siguiente código javascript:

if (url.match(/?rows.*?(?=\&)|.*/g)){
        urlset= url.replace(/?rows.*?(?=\&)|.*/g,"rows="+document.getElementById('rowcount').value);
    urlset= url+"&rows="+document.getElementById('rowcount').value;

Consigo el error invalid quantifier en el /?rows.*?.... This same regex works when testing it on using the test string


In this string, the above regex is supposed to match:


I actually don't even need the /? to get it to work, but if I don't put that into my javascript, it acts like it's not even regex... I'm terrible with Regex period, so this one has me pretty confused. And that error is the only one I am getting in Firefox's error console.


Using that link I posted above, it seems that the leading / tries to match an actual forward slash instead of just marking the code as the beginning of a regex statement. So the ? is in there so that if it doesn't match the / to anything, it continues anyway.


Ok, so in the end, I had to change my regex to this:


This matched the word "rows=" followed by anything until it hit an ampersand or ran out of text.

preguntado el 01 de febrero de 12 a las 22:02

What is it that you expect the leading "?" to mean? -

I'm using it so that it doesn't return false even if the / doesn't match anything. -

Well then you want to put the rest of the regex in parentheses and put the "?" después de that, not before it. Quantifiers always come después de the thing they refer to. -

Also the leading "/" does not try to match a "/" character. It's part of the native JavaScript syntax for regular expression constants. -

@Pointy That's what I thought, but I couldn't get it to work on that website with it. However, it may just be because I don't have to specify that part because the site adds it for me. It wasn't really clear. I'll try it without the ? when I get to work tomorrow. You should go ahead and post that as an answer, though. -

3 Respuestas

Necesitas escapar del primero ?, since it has special meaning in a regex.

//   ^---escaped

Respondido 02 Feb 12, 02:02

I don't want it escaped. I currently have it in because without the first /, the script doesn't recognize it as regex. So I added the ? so that it still works if it finds it 0 times. I'm not trying to match the actual question mark in the string. Thanks though. - James

@James: You're talking about the very first /, right? That's not part of the regex itself. It's just part of the literal notation used to create a regex. If you want it to work if it finds 0 items, then why use the regex at all? I think you need to take a step back and describe what you're actually trying to achieve. - user1106925

@Borodin: Sure it does. The asker hasn't clearly defined what should be matched, so my answer explains the cause of the error noted in the question. From the comments, it seems the asker was trying to inhabilitar the regex by using ?. Clearly this won't work. I'm awaiting clarification. - user1106925

regtest.htm produces

new RegExp("?rows.?(?=\&)|.", "") returned a SyntaxError: invalid quantifier

The value you put into the web site shouldn't have the / delimiters on the regex, so put in ?rows.*?(?=\&)|.* and it shows the same problem. Your JavaScript code should look like

re = /rows.*?(?=\&)|.*/g;

or similar (but that is a pointless regex as it matches everything). If you can't fix it, please describe what you want to match and show your JavaScript

Respondido 02 Feb 12, 03:02

Yeah, that's what Pointy said in the comments too. I think this is my problem. The online regex tester had me confused. If they don't post theirs as an answer, I'll just mark this as an answer. But if they do, I'll have to mark theirs since they technically answered it first. Thanks though. - James

You might consider refactoring you code to look something like this:

var url = "sort=notaryname&pageno=0&rows=anything&Start=0"
var rowCount = "foobar";
if (/[\?\&]rows=/.test(url))
    url = url.replace(/([\?\&]rows=)[^\&]+/g,"$1"+rowCount);



Respondido 02 Feb 12, 03:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.