Brent
Brent

Reputation: 2485

Regex Match Javascript Numbers

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

Upvotes: 4

Views: 340

Answers (1)

Alex K.
Alex K.

Reputation: 175956

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

Upvotes: 5

Related Questions