0

I want to catch numbers which are placed in text after word "NETTO-PRICE:" (there are few spaces in between too).

Text (eventually this text has got 2 lines/rows):

PRODUCENT: AMD MODEL-LINE: ATHLON CORES: X2 MODEL: 7750 FREQ: 2.6 CORES: 6 THREADS: 2 NETTO-PRICE: 250.00 QUANTITY: 7 PRODUCENT: INTEL MODEL-LINE:i7 CORES: X4 MODEL: 720QM FREQ: 2.8 CORES: 4 THREADS: 8 NETTO-PRICE: 450.00 QUANTITY: 5

I tried this: string.match(/[0-9]/g);but it catches all numbers in this text, when i only want those numbers after NETTO-PRICE:

This however doesn't work, cause it shows null: string.match(/NETTO-PRICE:[0-9]/g);

And if i am trying to concatenate this "arguments" in match() method, error comes up.

[edit]

I want to get only this numbers (as price), cause later i will be calculating brutto price and difference between netto (this numbers) and calculated brutto.

[edit2] How to change this code below to make it work for variable with whole text, not just text in quotes? Something like var s = string.split(' ');And string contains whole text and furrther the code will be like:

var s = 'NETTO PRICE: 321.321'.split(' ');
if(s[s.indexOf('NETTO') + 1] === 'PRICE:')
    console.log(s[s.indexOf('NETTO') + 2])
else
    console.log('fake');
Chris92
  • 489
  • 2
  • 13
  • 38

2 Answers2

1

In this case you could use regular expression with non-capturing group (see related question on stackoverflow). Regular expression could look similar to this one: /(?:NETTO-PRICE: )([0-9]+)/g

Community
  • 1
  • 1
Stubb0rn
  • 1,229
  • 12
  • 17
  • It works, but i don't want to output "NETTO-PRICE:" with numbers, but only numbers which are behind this word. Of course it can be other way than regex (match() method) – Chris92 Oct 22 '15 at 20:14
  • Since regexp contains `g` flag `String.prototype.match()` is not returning match objects. It should be used with `RegExp.prototype.exec()` method to get proper matches. – Stubb0rn Oct 22 '15 at 20:37
  • So how to solve it in other way than with regex? With split() or slice() maybe? – Chris92 Oct 22 '15 at 20:47
  • RegExp solution should work perfectly fine in this case. Just create RegExp and use its `exec()` method on string that contains numbers `var matches = /(?:NETTO-PRICE: )([0-9]+)/g.exec(string);` . `matches` will be an array where all items except the first one will be matched numbers. – Stubb0rn Oct 22 '15 at 20:52
  • This time i got output : **NETTO-PRICE: 250,250**, so it cuts decimal ending of number and also second num should be 450.00 not 250. – Chris92 Oct 22 '15 at 21:05
  • Take a look at this [jsfiddle](http://jsfiddle.net/Stubb0rn/jhqc8yos/1/) that contains complete example of using `RegExp.exec()` – Stubb0rn Oct 22 '15 at 21:26
  • `var re = new RegExp(/(?:NETTO-PRICE:\s)([0-9\.]+)/g); var match; while(match = re.exec(string)) { console.log("Text...:"+match[1]); }` This version of Yours code works, because i need to output this to console.**However** why are these numbers in match[1] kind of Array? – Chris92 Oct 22 '15 at 21:38
0

I solved it like this:

var re = new RegExp(/(?:NETTO-PRICE:\s)([0-9\.]+)/g);
var match, i;
var nettoP = new Array();
while(match = re.exec(string)) {
      nettoP[i] = match[1];
      console.log("Text...: "+nettoP[i]);
      i++;
}
var nettoPrice = new Array();
var counter = 0;
for (i = (nettoP.length)-1; typeof nettoP[i] == 'string' ; i--)
{
    nettoPrice[counter] = nettoP[i];                        
    console.log("netto Price: "+nettoPrice[counter]);
    counter++;
}
nettoPrice.reverse();
console.log("Netto number: "+ nettoPrice);

Thank you @Stubb0rn for help with Regex code.

Chris92
  • 489
  • 2
  • 13
  • 38