1

I need get two values from a string and sum them.

var text = 'SOC 01 672 1.653.806,08 18.512,98 1.667.621,57 2.647,38 07 23 12.965,11 0,00 12.965,11 0,00 13 5 10.517,81 0,00 10.517,81 0,00';
var reg  = new RegExp('SOC 01' +  '\\b.*?(?:\\d\\S*\\s+){3}(\\d\\S*)', 'i');
var reg2 = new RegExp('SOC 01' +  '\\b.*?(?:\\d\\S*\\s+){7}(\\d\\S*)', 'i');
var match = reg.exec(text);
var match2 = reg2.exec(text);

Output:

match[1] -> '1.667.621.57'
match2[1] -> '12.965.11'

(OBS: If you try run in the console the last number will have a comma, but in my node.js environment is a dot.)

I would like to know if i can create just one regex with two groups and sum them together.

The total should be 1.691.104,49

I was trying to sum like this:

(+match[1]) + (+match2[1])

output: NaN

Thanks.

2 Answers2

0

You're going to have to do something like:

Number(match[1].replace(/[\.,]/g, ''))

To strip out the .s and ,s before summing the numbers.

If you need to get the number back into the same format, you can do something like this:

var total = Number(match[1].replace(/[\.,]/g, '')) + Number(match2[1].replace(/[\.,]/g, ''));

var result = String(total);
result = result.replace(/(?=\d{2}$)/, ',');
result = result.replace(/\B(?=(\d{3})+(?!\d))/g, '.');

Which produces 1.680.586,68. I have the same question as stribizhev about how you are doing the summing.

Here is the fiddle for the above example: https://jsfiddle.net/qw700fq9/

Here is the updated fiddle - cleaned it up a bit: https://jsfiddle.net/rdu15ds3/

Also, adding the decimals back in was based on the answers to this question: How to print a number with commas as thousands separators in JavaScript

Community
  • 1
  • 1
lintmouse
  • 5,079
  • 8
  • 38
  • 54
  • Worked! Thanks dustmouse! –  Sep 02 '15 at 20:03
  • Great, thank you very much. Now i just need replace the two regex with one capture group each for one regex with two capture group. Do you know how? –  Sep 02 '15 at 20:59
  • Do you still care about the decimals and comma? – lintmouse Sep 02 '15 at 21:06
  • What are you trying to capture in each group specifically? – lintmouse Sep 02 '15 at 21:10
  • The same result, i want pass the word SOC 01 and get the third and seventh value after words or numbers. The same result but with only one regex. Possible? –  Sep 02 '15 at 21:12
  • Can you show me what you've tried and I will try to help out. – lintmouse Sep 02 '15 at 21:18
  • unfortunately, i was trying to find something related that could help because i don't know where to start. But no problem @dustmouse, you already help me a lot! –  Sep 02 '15 at 21:19
  • Try this: SOC\s01\s(?:\S*\s+){3}(\S*)(?:\S*\s+){4}(\S*). – lintmouse Sep 02 '15 at 21:59
  • this wont work i think - var reg = new RegExp('SOC\s01\s(?:\S*\s+){3}(\S*)(?:\S*\s+){4}(\S*).', 'i'); –  Sep 03 '15 at 13:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88714/discussion-between-dustmouse-and-amanda-ferrari). – lintmouse Sep 03 '15 at 14:15
0

The regex is returning a string. You need to convert them to numbers before adding them.

function toNumber(n){
    return parseFloat(n.replace(/./g,'').replace.(/,/g,'.'));
}

toNumber(+match[1]) + toNumber(+match2[1]);
David Bradshaw
  • 11,859
  • 3
  • 41
  • 70