Learn how to easily extract all or the last group of numerical digits in any string with JavaScript using RegExp.

In this short article, I will explain you two easy ways to obtain the numerical groups of any string with JavaScript.

Capturing all the groups of numerical digits

In case that you need to extract all the numbers from a string, independently from its length or the amount of groups:

Capturing Numerical Groups from String JavaScript

You may use the following function that returns the result of the string matching against the /\d+/g regular expression:

  • \d Digit. Matches any digit character (0-9).
    • + Quantifier. Match 1 or more of the preceding token.
/**
 * Returns an array of all the numerical groups in the given string.
 * 
 * @param {String} str The input string 
 * @returns {Array} An array of all the numerical groups (in string format) in the given string.
 */
function getAllNumericalGroups(str) {    
    let lastGroupOfNumbers = str.match(/\d+/g);

    return (lastGroupOfNumbers ? lastGroupOfNumbers : []);
}

// ['123', '12']
console.log(getAllNumericalGroups('ABC-123-X12'));

// []
console.log(getAllNumericalGroups('ABC'));

// ['799', '3445']
console.log(getAllNumericalGroups('ABC-799-WWW3445X'));

// ['23', '419', '51']
console.log(getAllNumericalGroups('23HL-X419E-VHL51E'));

Capturing the last group of numerical digits

If you're looking for a solution that simply returns the last group of numerical digits from a string, independently from its length:

Extract Last Group Of Numbers JavaScript

You may use the following function that returns the result of the string matching against the /(?:\d+)(?!.*\d)/ regular expression:

  • (?: Non-capturing group. Groups multiple tokens together without creating a capture group.
    • \d Digit. Matches any digit character (0-9)
    • + Quantifier. Match 1 or more of the preceding token.
  • (?! Negative lookahead. Specifies a group that can not match after the main expression (if it matches, the result is discarded).
    • . Dot. Matches any character except line breaks.
    • * Quantifier. Match 0 or more of the preceding token.
    • \d Digit. Matches any digit character (0-9).
/**
 * Returns a string with the last group of numbers in the string.
 * 
 * @param {String} str The input string 
 * @returns {String} String that contains the last group of numbers in the input string.
 */
function getLastNumericalGroup(str) {    
    let lastGroupOfNumbers = str.match(/(?:\d+)(?!.*\d)/);

    return (lastGroupOfNumbers ? lastGroupOfNumbers[0] : null);
}

// "12"
console.log(getLastNumericalGroup('ABC-123-X12'));

// null
console.log(getLastNumericalGroup('ABC'));

// "3445"
console.log(getLastNumericalGroup('ABC-799-WWW3445X'));

// "51"
console.log(getLastNumericalGroup('23HL-X419E-VHL51E'));

With the previous examples, you can do a lot of things that will satisfy regular needs like:

  • Extract the last numeric digit from a string.
  • Add all the numbers in a text string either individually or by groups.
  • Create an array with all the digits of a string using join and split.
  • You name it!

Remember that all the groups or numeric values are returned in strings, so be sure to parse them as integers using parseInt.

Happy coding ❤️!


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors