var matches = destinationClass.match(/item-([0-9]+)/);
console.log(matches[1]);
matches[1] gives the substring corresponding the part of pattern enclosed in parentheses
Question
I want to match the index of a CSS class.
Example:
Given string: .item.item-3.layer.layer-1.test
Desired result: 3
(the index following the class "item")
But my current RegEx just gives me the index of the last class with a dash:
var matches = destinationClass.match(/[^-]+$/);
console.log(matches[0]);
Can anyone help me?
Solution
var matches = destinationClass.match(/item-([0-9]+)/);
console.log(matches[1]);
matches[1] gives the substring corresponding the part of pattern enclosed in parentheses
OTHER TIPS
You could simplify your regex to:
match(/item-([0-9]+)/).pop()