1

I have read some answers to similar questions but I am still not sure how to do it in my case.

I am creating a search whole word function. Because the keyword is saved in an array so I need to pass it as a template literal within a loop.

I use \b to ensure nothing before the start of the word and the word may finish with "." or empty space so I put \s*.? If my regular expression is wrong please help me fix it.

let text = 'I saw a afox foxa in the wood.no fox. The had red fur.';
let word = "fox"
const regex2 = new RegExp(`\b${word}\s*.?`)
console.log(text.search(regex3));

the result keep showing found nothing. My syntax must be wrong in combining regular expression and template literal, please let me know how to fix it. thank you

cass
  • 37
  • 5

0 Answers0