Sum all numbers in text using regexp I would like to improve everything that can be improved. I don't understand regex a lot and what could be wrong. The main improve that I am looking for is related with errors. I want make the code robust to deal well with all kinds of errors. I could try to run in production and wait to see the errors, but I can't. At my company, I need to try to imagine all possible scenarios, good or bad.

Codepen

'use strict';

var sum = function(text, regex) {

  var match;
  var regex = regex || /(\d+)/g;

  var result = []
  while (match = regex.exec(text)) {
    result.push(match[1]);
  }

  return result.reduce(function(prev, curr) {
    return parseInt(prev) + parseInt(curr);
  }, 0);
};

console.log(sum('1 12 40'));
console.log(sum('10 12 40'));
console.log(sum('1 12 510'));
console.log(sum('LLLLLLLL'));
console.log(sum('10 1'));
Numbers or integers ?

First of all, I notice that your question title talks about numbers, while your code looks only for positive integers.

In other words, it's currently not intended to take in account numbers such as 12.34 or -123.
To also detect and use this kind of numbers, your function would need a much more complex process.

Let's say you really want to remain limited to integers (I think so especially because of your parseInts). Then you may simplify your code in several ways.

Utility of regexargument ?

First I don't see why you're using a regex argument: for the purpose of your function, seems that it can't be anything else than the default you gave to it.
But this is up to you, and I'll keep this argument in what follows.

Declaring regex variable.

So just regarding it, you don't need to declare a new variable to use it: the regex argument your received has its scope limited to the function, and you may modify it directly. So you can simply write:

regex || /(\d+)/g;


Choosing the more efficient regex expression

Now regarding the way you expressed the regex: with its capturing parentheses, /(\d+)/g it makes your regex.exec(text) to return only one matching block each time, forcing you to the while() where you extract it.

At the opposite, using /\d+/g, you can merely do text.match(regex) and it'll return all matching blocks at once. Then you can directly reduce() this resulting array, though having to take care of the case it's null (like with your 'LLLLL').

Casting to numbers

While reduce() works, passed blocks contain only digits but are strings, so you're right to ensure they're processed as number instead.
But rather than parseInt (really useful only to extract integer part from a string where there are also other characters), you can merely cast curr to a number with +curr.

Using reduce().

You can take advantage of this reduce() capability: if you omit its 2nd argument (initial value for prev), then the 1st step is that curr is automatically affected to prev.
Note that in this case you must alos cast +prev!

Final reduced suggestion.

Last point, you can reduce the whole code by directly using the match() result in the reduce() process, without having to use other intermediate variables, assumed you take care of the possible null result from match().

Here is the suggested version:



'use strict';

var sum = function(text, regex) {
  var match;
  return !!(match = text.match(regex || /\d+/g)) ?
    match.reduce(function(prev, curr) {
      return +prev + +curr;
    })
  : 0;
}

console.log(sum('1 12 40'));
console.log(sum('10 12 40'));
console.log(sum('1 12 510'));
console.log(sum('LLLLLLLL'));
console.log(sum('10 1'));