1

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 Answers 2

2

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];
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
0

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

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

Comments

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.