0

I made a regex to highlight keywords found in a text by another tool.

new RegExp(highlightedKeywords.map(v => `\\b${v}\\b`).join('|') || /.^/, 'gi'), match => `<mark>${match}</mark>`)

I would like to make this regex treat every whitespaces as the same because the tool who extract keywords from the text convert every whitespace as space, so for example I have a keyword "the cat" not found because the actual text is "the\ncat".

I don't want to ignore whitespaces because "the cat" should not match "thecat" but I would like to match "the\tcat" or even "the\n \tcat"

Gatoyu
  • 642
  • 1
  • 6
  • 22

1 Answers1

0

Answer found thanks to pwilcox : "You're looking for \s"

new RegExp(highlightedKeywords.map(v => `\\b${v.replace(/\s/g, '\\s')}\\b`).join('|') || /.^/, 'gi')`

I replace every whitespace in the keywords by \s

Gatoyu
  • 642
  • 1
  • 6
  • 22