I have attribute value as:
<div id = "2fComponents-2fPromotion-2f" class = "promotion">
Now I want to get only portion of it, say Promotion and its value 2f, how can I get this using jquery ? Do we have built in function for it ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use a regular expression here:
This assumes that the “value” of
Promotionis a hexadecimal value and the characters [a-f] will always be lower case. It’s also easily adjusted to match other values, for instance, if I change the regex to/component-([0-9a-f]{2})/, the match array would be["component-3a", "3a"].The match method takes a regular expression as its input and searches the string for the results. The result is returned as an array of matches, with the first index being the complete match (equivalent regex for this only would be
/Promotion-[0-9a-f]{2}/). Any sub-expression (expressions enclosed in parenthesis) matches are added to the array in the order they appear in the expression, so the(Promotion)part of the expression is added to the array at index 1 and([0-9a-f]{2})is added at index 2.matchmethod on MSDN