views:

35

answers:

2

I have following div:

<div id = "zone-2fPromotion-2f" class = "promotion">

How can I get value 2f present in the end, actually it is the value of Promotion and how can I retrieve it ?

I was using

this.match = this.id.match(/(Promotion)-([0-9a-f]{2})/);

but it is not giving me exact result but it gives me array of (Promotion-2f, Promotion, 2f) but this is not what I require. Any guidance and also if any one can refer me to good online resource for regex, it would highly helpful and appreciated.

+2  A: 

that's what you want. the results of the match are firstly the entire matched part of the expression, and then the various groups. so since you want to retrieve group 2 (the second lot of ()'s ) just do

this.match = this.id.match(/(Promotion)-([0-9a-f]{2})/)[2];

also, the best online reference i've found is http://www.regular-expressions.info/

edit: you could just leave out the ()'s around Promotion, since you're not worried about returning that part, and do this

this.match = this.id.match(/Promotion-([0-9a-f]{2})/)[1];
oedo
+1, now I understand how it works.
Rachel
excellent. if you have a moment, have a read through the tutorial on that regular-expressions.info site, it's incredibly thorough and very well explained.
oedo
Yes. I will go through it. I always wanted to learn regular expression as they are very powerful tool at developer's disposal. But somehow was not getting any proper resource to start with...also am not too much into books and so didn't wanted to go through Mastering Regular Expression Book.
Rachel
Please mark it answered!
Juan Mendes
A: 

This is a good resource on regular expressions for all languages.

http://www.regular-expressions.info/javascript.html

harpo