0

I need to use a regular expression in javascript to match certain text in a url.

Here is an example:

"qty1=0&qty2=0&qty3=0&"

I want to match the text(number) that comes after the '=' and before the '&' whenever the word 'qty' appears in the string. The text(number) will be subject to change every time the url is generated, so I want to match whatever the new text(number) may be.

What regular expression could I use to solve this?

Any help would be appreciated!

1
  • I need to clarify a bit. Match was the wrong word to use, my apologies. I simply need to use a regular expression to find whatever text(number) comes between "=" and "&" whenever "qty" shows up in the text string. I then need to store this text(number) in a variable so I can run some conditional statements against it. Commented Feb 2, 2012 at 3:40

3 Answers 3

1
var queryStr = "qty1=0&qty2=0&qty3=0";
var regex = /qty(\d+)=(\d+)/g;
var match = regex.exec(queryStr);
while(match != null){
  var numBeforeEquals = match[1];
  var numAfterEquals = match[2];
  // do your comparison, for example, if it's > 0:
  if(numAfterEquals > 0){
     // do something
  }
  match = regex.exec(queryStr);
}

Note the /g at the end of the regex, in some javascript implementations you will end up with an infinite loop without it.

Sign up to request clarification or add additional context in comments.

Comments

0

try this

var regex = /qty[\d]+=(\d+)/g,
    text  = "qty1=5&qty2=7&qty3=8&";

while(match = regex.exec(text)) {
    console.log(match[1]);
}

@ggreiner missing + in \d that make the script above does not work with number greather than 9 e.g. 10, 11

2 Comments

This definitely gives me number beyond 0-9 between "qty" and "=" as well as "=" and "&" (as far as I can tell). But the regular expression matches "qty1=0","qty2=0","qty3=0" instead of just the number after "=" as in "0","0","0".
You missed my point match[1] this is what you want (match only that number).
0

Here is an alternative solution (inside a working URL). I call it alternative because I'm using the parts of the expression twice, I think it could be optimized.

var breakdown = "http://local/?wtf=omg&qty12=a&qty2=b&qty3=4&test=5".match(/qty[0-9]{0,9999}(.*?)&/ig);
for(var i in breakdown) {
   var v = breakdown[i].match(/qty[0-9]{0,9999}\=(.*?)&/i)[1];
   if(v == 4 || v == "a") {
      alert("Found '"+v+"'!")
   }
}

Working Example: http://jsfiddle.net/MattLo/FxDyg/

2 Comments

This is almost exactly what I need. Is there a way to change the regular expression to match any number after "qty" and the "="? Right now it only checks numbers 0-9. Also, is there a way I can capture each value individually so I can run a conditional if statement against each value in order to run separate blocks of code?
Yes and yes, I updated the snippet to accept up to 9999 and an area for your conditional statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.