0

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?

2 Answers 2

1
var matches = destinationClass.match(/item-([0-9]+)/);
console.log(matches[1]);

matches[1] gives the substring corresponding the part of pattern enclosed in parentheses

Sign up to request clarification or add additional context in comments.

Comments

1

You could simplify your regex to:

match(/item-([0-9]+)/).pop()

3 Comments

But then it grabs "item-3" but I just want to have the "3".
Then add .pop() to the end
Thanks. To use pop() is very smart. Thank you very much! :)

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.