Home > OS >  Javascript regex invalid quantifier error to find 8 digit number in PDF
Javascript regex invalid quantifier error to find 8 digit number in PDF

Time:01-27

I have the following javascript code:

/* Extract pages to folder */

// Regular expression used to acquire the base name of file
var re = /\.pdf$/i;

// filename is the base name of the file Acrobat is working on
var filename = this.documentFileName.replace(re,"");

try {for (var i = 0; i < this.numPages; i  )

    var id = /\ (?<!\d)\d{8}(?!\d)/;
    console.println(id);

    this.extractPages({

    nStart: i,

    cPath: "/J/my file path/"   "SBIC_"   id   ".pdf"

    });        

} catch (e) { console.println("Aborted: "   e) }

I get the error that the quantifier is invalid in this line of code var reg = /\ (?<!\d)\d{8}(?!\d)/

However, this line of regex pulls the id 22001188 when I use it in https://regex101.com/ to find the 8 digit number in "I.D. Control 22001188".

Do I have to integrate the regex a different way in the code for it to search through the text in the document?

CodePudding user response:

The sequence ?<! is a negative look-behind sequence which is not yet supported by all the browsers/systems. It seems that it is not supported in your case.

You may use word boundaries in regex as given below to extract 8-digit numbers from your string:

\b\d{8}\b

CodePudding user response:

Those (?<!\d) and (?!\d) are probably the problem. They are only supported in some regex libraries.

You can instead use ^\d{8}$ to match 8 digits at the start and end of the line, or \b\d{8}\b to match 8 digits surrounded by word boundaries, as said in ayush-s answer.

  • Related