-1

I have the following javascript code:

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

I get the error invalid quantifier at the /?rows.*?.... This same regex works when testing it on http://www.pagecolumn.com/tool/regtest.htm using the test string

?srt=acc_pay&showfileCL=yes&shownotaryCL=yes&showclientCL=no&showborrowerCL=yes&shownotaryStatusCL=yes&showclientStatusCL=yes&showbillCL=yes&showfeeCL=yes&showtotalCL=yes&dir=asc&closingDate=12/01/2011&closingDate2=12/31/2011&sort=notaryname&pageno=0&rows=anything&Start=0','bodytable','xyz')

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

rows=anything

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.

EDIT

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.

RESOLUTION

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

/rows=.*(?=\&?)/g

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

James
  • 3,765
  • 4
  • 48
  • 79
  • What is it that you expect the leading "?" to mean? – Pointy Feb 01 '12 at 22:24
  • I'm using it so that it doesn't return false even if the `/` doesn't match anything. – James Feb 01 '12 at 22:45
  • Well then you want to put the rest of the regex in parentheses and put the "?" **after** that, not before it. Quantifiers always come **after** the thing they refer to. – Pointy Feb 01 '12 at 22:49
  • Also the leading "/" does not try to match a "/" character. It's part of the native JavaScript syntax for regular expression constants. – Pointy Feb 01 '12 at 22:50
  • @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. – James Feb 01 '12 at 22:53
  • *"Using that link I posted above..."* Forget the link. No one is going to care about what an online regex validator tells you. Focus on describing what you're trying to ultimately achieve. –  Feb 01 '12 at 22:53
  • Well I'd be happy to post an answer but it'd help a lot if you'd describe in plain English what it is that you want to match. – Pointy Feb 01 '12 at 22:55
  • @Pointy I already posted that in an edit. I just meant you should post that the leading `/` is part of the regex code, and not part of the matched string. I think that's what my problem is. I was thinking it was part of the matched string. – James Feb 01 '12 at 23:21
  • @amnotiam I had already done that in the edit. – James Feb 01 '12 at 23:23

3 Answers3

4

You need to escape the first ?, since it has special meaning in a regex.

   /\?rows.*?(?=\&)|.*/g
//   ^---escaped
  • 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 Feb 01 '12 at 22:45
  • @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. –  Feb 01 '12 at 22:51
  • @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 *disable* the regex by using `?`. Clearly this won't work. I'm awaiting clarification. –  Feb 01 '12 at 23:22
2

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

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • 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 Feb 01 '12 at 23:25
0

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);
}
console.log(url);

Output

sort=notaryname&pageno=0&rows=foobar&Start=0

Sam Greenhalgh
  • 5,952
  • 21
  • 37