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

**URL:** <https://pdfjs.community/t/can-we-use-textsearchinit-pattern-mode-searchoptions-for-a-regex-as-pattern/409>\
**Category:** Technical Support\
**Created:** [December 8, 2020, 11:21am UTC](https://pdfjs.community/t/can-we-use-textsearchinit-pattern-mode-searchoptions-for-a-regex-as-pattern/409 "2020-12-08T11:21:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![himani.sharma](https://avatars.discourse-cdn.com/v4/letter/h/e47774/32.png) [@himani.sharma](https://pdfjs.community/u/himani.sharma)\
**Post date:** [December 8, 2020, 11:21am UTC](https://pdfjs.community/t/can-we-use-textsearchinit-pattern-mode-searchoptions-for-a-regex-as-pattern/409/1 "2020-12-08T11:21:23Z")

</div>

**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! 

      }

    })
  })
```

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [December 14, 2020, 6:25pm UTC](https://pdfjs.community/t/can-we-use-textsearchinit-pattern-mode-searchoptions-for-a-regex-as-pattern/409/2 "2020-12-14T18:25:01Z")

</div>

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:

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

```

Thanks!  
Logan

---

<div class="post-metadata">

**Author:** ![himani.sharma](https://avatars.discourse-cdn.com/v4/letter/h/e47774/32.png) [@himani.sharma](https://pdfjs.community/u/himani.sharma)\
**Post date:** [December 15, 2020, 6:07am UTC](https://pdfjs.community/t/can-we-use-textsearchinit-pattern-mode-searchoptions-for-a-regex-as-pattern/409/3 "2020-12-15T06:07:40Z")

</div>

Hey Logan ,

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