Przemek
Przemek

Reputation: 317

JScript Regex - extract 10-digit VAT number with seperators procedeed by a substring

I need to extract VAT number that consists of 10 digits seperated by a dash, space or not seperated and procedeed by substring 'VAT'. VAT number never starts start with zero and is always followed by a space.

reg = new RegExp('[\d -]{10}\\s','ig');
str = ('sdfgzdfvzdfv/9/2020 VAT Invoice 16-09-2020 Citsxc zzzzw34  224- Iscvcge date 16.09.2020 VAT Terms 34 of payment:  123-456-78-90 *')
expected result: '123-456-78-90'

Thank you.

Upvotes: 1

Views: 162

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627044

You may use

var rx  = RegExp('VAT.*?([1-9](?:[\\s-]?\\d){9})(?!\\S)','i');

See the regex demo. Details:

  • VAT - a VAT substring
  • .*? - any 0+ chars other than line break chars, as few as possible
  • ([1-9](?:[\s-]?\d){9}) - Group 1:
    • [1-9] - a non-zero digit
    • (?:[\s-]?\d){9} - 9 occurrences of a whitespace or hyphen and then a digit
  • (?!\S) - a right-hand whitespace boundary.

JScript code demo:

var rx  = RegExp('VAT.*?([1-9](?:[\s-]?\\d){9})(?!\\S)','i');
var string=('sdfgzdfvzdfv/9/2020 VAT Invoice 16-09-2020 Citsxc zzzzw34 224- Iscvcge date 16.09.2020 VAT Terms 34 of payment123-456-78-90 ');
var m = string.match(rx);
if (m) {
  WScript.Echo(m[1]);
} else {
  WScript.Echo("No match!");
}

Output: 123-456-78-90.

Upvotes: 1

Related Questions