JavaScript/REGEX: Non-capturing matches

I would like to place all matching numbers in an array.  I do not want the prefixes to be included.  
xyz = "Hello World quote_price_standard-1 testing 3 hello_world-11 #foo#bar 253 sdfsdf-532 test_standard-111 ghdghdh-932 "
xyz = xyz.match(/((_[a-zA-Z]+\-))([0-9])+/g);
alert(xyz); // Should return 1,11,111

Open in new window

I understand that ?: can be used for non-capturing groups.  How can I capture only the number at the end without capturing the prefix?
LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Alexandre SimõesManager / Technology SpecialistCommented:
Like this it will get all numbers with letters before:
xyz = xyz.match(/([a-zA-Z]+)?([0-9])+/g);

Open in new window

Dan CraciunIT ConsultantCommented:
See my other solution: http://www.experts-exchange.com/questions/28711270/JavaScript-REGEX-Largest-Matching-Number-in-String.html

The "numbers" array will contain what you want.

HTH,
Dan
skijAuthor Commented:
No, Alexandre Simões, the code you posted grabs too many numbers!  Only 1, 11, and 111 should be grabbed.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

skijAuthor Commented:
Dan Craciun, it seems unnecessary to loop through the results.  Can't this be accomplished with  pure regex and non-capturing matches?
Dan CraciunIT ConsultantCommented:
You might find this useful: http://www.regular-expressions.info/tutorial.html

If you don't want something to be in a capture group, don't enclose it in parenthesis.
Dan CraciunIT ConsultantCommented:
See here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/match

Relevant info:
"If the regular expression includes the g flag, the method returns an Array containing all matched substrings rather than match objects. Captured groups are not returned."
Dan CraciunIT ConsultantCommented:
You need to use regexp.exec for capturing groups and yes, you have to iterate through results:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
Alexandre SimõesManager / Technology SpecialistCommented:
@skij: sorry for my reply being in error and @Dan Craciun's reply is correct.

In fact, you're not looping through the results, you are actually running the regex query again from the previous match. The regex object keeps track of it's results and knows where to start from in the next iteration using a property called lastIndex

I copied @Dan Craciun's code and added a console.log with that propery value for you to better understand
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) {
    console.log(myRegexp.lastIndex);
    
    numbers.push(match[1]);
    match = myRegexp.exec(myString);
}

console.log(numbers);

Open in new window

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
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
JavaScript

From novice to tech pro — start learning today.