4

I have tried a few different techniques to match just numbers from my DIV but keep failing.

Currently my example html looks like the following.

<div data-video="22" class="stars starrr"></div>
<div data-video="/test/22" class="stars starrr"></div>
<div data-video="/someother/test/22" class="stars starrr"></div>

I need to only match the number in video, but I keep getting it to work with some and failing with others.

My current code

$(this).data("video").match(/([\d]+)/);

Current Error on first match The others work

Uncaught TypeError: undefined is not a function

2
  • 1
    I have added an example above. Commented Dec 8, 2014 at 16:49
  • I hope that explains why you get the error: jsfiddle.net/ryfgydee Commented Dec 8, 2014 at 16:50

1 Answer 1

5

You need to cast to a string in the first case;

var f = $(this).data("video").toString().match(/([\d]+)/);

as typeof $(this).data("video") is number so no .match

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

2 Comments

you don't need to put \d inside a character class. And i think you don't need to have capturing group also.
Oh you legend, I been scratching my head for hours trying to work out why. Thanks

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.