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

0
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
0
skijAuthor Commented:
No, Alexandre Simões, the code you posted grabs too many numbers!  Only 1, 11, and 111 should be grabbed.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

skijAuthor Commented:
Dan Craciun, it seems unnecessary to loop through the results.  Can't this be accomplished with  pure regex and non-capturing matches?
0
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.
0
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."
0
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
0
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

0

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.