Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17566

how to get integer values in a string with js

var text = "rating-1-gray-3-blue";

i want to get value 13

how can i do this . basically i want to get all integers inside a string .

i tried with parseInt , but it returns int when the integer value is at the beginning of the string

thanks in advance

Upvotes: 1

Views: 231

Answers (2)

Hanky Panky
Hanky Panky

Reputation: 46900

var result=text.replace(/[^0-9]/g, ''); 

This removes all non 0-9 values from your string

Upvotes: 2

VisioN
VisioN

Reputation: 145368

You may use regular expressions:

var result = +text.replace(/\D/g, "");  // 13

Upvotes: 4

Related Questions