Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Regular Expression multiple match [duplicate]

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
like image 561
user815460 Avatar asked Oct 31 '11 12:10

user815460


1 Answers

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.

like image 177
alex Avatar answered Nov 08 '22 17:11

alex