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 .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.
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./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.
Share Improve this question edited Feb 2, 2012 at 14:13 James asked Feb 1, 2012 at 22:20 JamesJames 3,8056 gold badges50 silver badges81 bronze badges 9- What is it that you expect the leading "?" to mean? – Pointy Commented Feb 1, 2012 at 22:24
-
I'm using it so that it doesn't return false even if the
/
doesn't match anything. – James Commented Feb 1, 2012 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 e after the thing they refer to. – Pointy Commented Feb 1, 2012 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 Commented Feb 1, 2012 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 Commented Feb 1, 2012 at 22:53
3 Answers
Reset to default 4You need to escape the first ?
, since it has special meaning in a regex.
/\?rows.*?(?=\&)|.*/g
// ^---escaped
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
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