There is a string coming from input , you need to make a regular list (an analogue of the search), but if we allow + or \ to be entered in the field, there will be an error creating a regular expression through new RegExp, because it accepts not a string, but a pattern, how can you avoid it? so that when I enter any data into input , I have created a regular search for this string, and not a pattern that was processed by ReqExp ? and is this the right approach?

  • тк она принимает не строку , а паттерн contrary. line it takes javascript.ru/regExp - Lexx918
  • if from any input line you need to get a valid pattern, then you just need to shield the special characters. the same plus or backslesh. - Lexx918
  • Eh, I hoped there was some other option) Thank you - MoJlo4HuK

1 answer 1

Screen the special characters in the string and you get a valid regular schedule.

 RegExp.escape= function(s) { return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); }; 

(C) https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript

  • one
    +1 for creating a regular expression using a regular expression - yar85