JavaScript/REGEX: Largest Matching Number in String

Using JavaScript and regular expressions, how can I find the largest number that matches the pattern of having an underscore followed by letters followed by a dash followed by a number?

The pattern should be something like this: /_(\w+)\-(\d+)/g

var xyz = 'Hello World quote_price_standard-1 testing 3 hello_world-5 253 sdfsdf-532 test_standard-123 ghdghdh-932';
alert( Math.max.apply(null, xyz.match(/(\d+)/g)) ); // Should be 123

Open in new window

LVL 10
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I can't imagine how you'd do that with a single regular expression. You can use a regular expression to capture candidate strings, but you'd need a separate bit of code to compare them for largest and pick out the one you want. Can you say more about this criterion? Is it the string with the largest numerical value after the dash or the string with the largest number of characters?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark BullockQA EngineerCommented:
You can use this regular expression. I don't know the javascript.

Open in new window

If you want to test the regular expression, I saved it here.
Dan CraciunIT ConsultantCommented:
@Mark: you basically rewrote the regexp from the OP...

@skij: try this:
var myString = 'Hello World quote_price_standard-1 testing 3 hello_world-5 253 sdfsdf-532 test_standard-123 ghdghdh-932';
var myRegexp = /_\w+\-(\d+)/g;
var match = myRegexp.exec(myString);
var numbers = [];
while (match != null) {
    match = myRegexp.exec(myString);

alert( Math.max.apply(null, numbers) );

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.