0

So straight to the point. I am currently making a college project that consists on search a JSON file for specific words/sentences according to 2 types of research: Restrict which means case sensitive and Lexical which means case insensitive And return the occurrencies in bold format

Currently i'm using this regex as a search method

regex = new RegExp(inputSearch, "g"); //case sensistive

and

regex = new RegExp(inputSearch, "gi"); //case insensitive

"inputSearch" beeing the variable that contains the sentence/word that0s supposed to be found, my problem is that i need to include special characters such as these ".:,;!?-_" so if, for example, i search for "How are you?" it would return all the occurrencies of that sentence including the "?"

Can anyone please help me?

Thank you in advance

1 Answers1

0

Just escape all the special characters present in your variable with string.replace function.

var regex = new RegExp(inputSearch.replace(/(\W)/g, "\\$1"), "gi");
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274