Can we use textSearchInit(pattern, mode [, searchOptions]) for a regex as pattern?

PDF.js Express Version

Detailed description of issue
Currently textSearchInit() function is working fine for a single string but not for a regex.

Expected behaviour
Need to apply this function for a regex.

Does your issue happen with every document, or just one?
{Answer here}

Link to document
{Provide a link to the document in question if possible}

Code snippet

  const { CoreControls } = instance;

  docViewer.on('documentLoaded', () => {
    let arr = [" ICO ", " 30/45 ", " 6AR ", " LD ", " 70D ", " 120 ", " EZ10 ", " TRW ", " CRI90 "]
    
  
    const regexFromMyArray = arr.join("|");

   
    const mode = CoreControls.Search.Mode.PAGE_STOP | CoreControls.Search.Mode.HIGHLIGHT;
    const searchOptions = {
      fullSearch: true, // search full document
      onResult: result => {

        if (result.resultCode === CoreControls.Search.ResultCode.FOUND) {

          const textQuad = result.quads[0].getPoints();
          const annot = new Annotations.TextHighlightAnnotation();

          annot.X = textQuad.x1;
          annot.Width = textQuad.x2 - textQuad.x1;

          annot.Y = textQuad.y3;
          annot.Height = textQuad.y1 - textQuad.y3;

          annot.FillColor = new Annotations.Color(0, 255, 0, 0.5);
          annot.StrokeColor = new Annotations.Color(255, 0, 0, 0.7);

          annot.Quads = [textQuad];

          // add custom property so we can check in annotationSelected function
          annot.IsCustomSearch = true;

          annotManager.addAnnotation(annot);
          annotManager.drawAnnotationsFromList([annot])

        }
      }
    };

    // start the search

    docViewer.textSearchInit(regexFromMyArray, mode, searchOptions);


  });

  // Listen for when our search annots are selected
  annotManager.on('annotationSelected', (annots) => {

    if (!annots) return;
    annots.forEach(annot => {

      // check that our custom 'IsCustomSearch' property is set
      if (annot instanceof Annotations.TextHighlightAnnotation && annot.IsCustomSearch) {

        const { X, Y, Width, Height } = annot;

        // Here you can use X, Y, Width, Height 
        // to create a new Rectangle Annotation! 

      }

    })
  })

Hey there!

Sorry for the late reply here, somehow this message slipped through the cracks.

You can add CoreControls.Search.Mode.REGEX to your search mode, like so:

const mode = CoreControls.Search.Mode.PAGE_STOP | CoreControls.Search.Mode.HIGHLIGHT | CoreControls.Search.Mode.REGEX;

Thanks!
Logan

Hey Logan ,

Thanks a lot! This really solved the problem I was having.