mylesagray
mylesagray

Reputation: 8869

Using regex to extract a double digit number from a string

I want to extract the "73" from this string:

wiring_details_1-6-73

The numbers 1, 6 and 73 are all vars so I need it to be value independant, I found this website which generated this regex:

.*?\\d+.*?\\d+.*?(\\d+)

But that seems a bit overkill and complicated, what is the cleanest method to extract the "73"?

Upvotes: 2

Views: 880

Answers (2)

Livingston Samuel
Livingston Samuel

Reputation: 2422

You can use this /\d{1,2}$/

var str = "wiring_details_1-6-73";

var result = str.match(/\d{1,2}$/);

this returns an array with the matched number as string and you can access the value as result[0].

Upvotes: 1

amosrivera
amosrivera

Reputation: 26514

var string = "wiring_details_1-6-73"
var chunks = string.split("-");
alert(chunks[chunks.length-1]) //73

demo: http://jsfiddle.net/eawBT/

Upvotes: 3

Related Questions