Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  87] [ 2]  / answers: 1 / hits: 47817  / 13 Years ago, mon, october 31, 2011, 12:00:00

I'm trying to use javascript to do a regular expression on a url (window.location.href) that has query string parameters and cannot figure out how to do it. In my case, there is a query string parameter can repeat itself; for example quality, so here I'm trying to match quality= to get an array with the 4 values (tall, dark, green eyes, handsome):



http://www.acme.com/default.html?id=27&quality=tall&quality=dark&quality=green eyes&quality=handsome

More From » regex

 Answers
11

You can use a regex to do this.



var qualityRegex = /(?:^|[&;])quality=([^&;]+)/g,
matches,
qualities = [];

while (matches = qualityRegex.exec(window.location.search)) {
qualities.push(decodeURIComponent(matches[1]));
}


jsFiddle.



The qualities will be in qualities.


[#89364] Friday, October 28, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
faithemilys

Total Points: 418
Total Questions: 100
Total Answers: 114

Location: North Korea
Member since Fri, Nov 4, 2022
2 Years ago
;