Javascript Regular Expression Groups

Hi,

I was wondering if there was any way of using groups in Javascript regular expressions. I know it can be done on the server side but I am having trouble getting it going on the client side.

For instance in .NET I would do the following

Regex regex = new Regex("[?|&]?(?<key>[^=]+)=(?<value>[^&]+)");

This would give me 2 groups on a match ..."key" and "value".

All suggestions much appreciated.

Paul

adavirAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
are try to get the key/value pairs from a querystring? If so refer to:
http://blog.falafel.com/2007/12/17/ParseAQueryStringInJavaScript.aspx

0
hieloCommented:
but yes, you can use parenthesis to group in javascript:
<script type="text/javascript">
var str="http://www.somesite.com?name=john&age=90&height=3#main";
var regex = new RegExp("[\\?&](([^=]+)=([^&#]+))","g");
var result = []; 
while( str.match(regex) )
{
	result[result.length]=RegExp.$1;
	str = str.replace( RegExp.$1, "" );
}
alert( result );
</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adavirAuthor Commented:
Good Job,

I was a bit baffled when I first read your code, I thought this can't be correct. What if there were concurrent RegExp instances. For instance if you look at the code attached the final alert would be "http"

Not a very OO way for Javascript to implement RegExp groups. But heh it works!

Thanks again, you obviously know your javascript

Paul
<script type="text/javascript">
var str="http://www.somesite.com?name=john&age=90&height=3#main";
var regex = new RegExp("[\\?&](([^=]+)=([^&#]+))","g");
var protocolRegex = new RegExp("(http|ftp|gopher)");
var result = []; 
while( str.match(regex) )
{
         var protocolMatch = str.match(protocolRegex);
 
	result[result.length]=RegExp.$1;
	str = str.replace( RegExp.$1, "" );
}
alert( result );
</script>

Open in new window

0
adavirAuthor Commented:
sorry the alert would be "http, http, http",
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.