Trying to understand and replicate a text highlighter in JS and came across this particular command:
textToSearch = textToSearch.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");
as shown in the photo below. What does the set of symbols followed by a "/g " mean ? What does “\$&” mean ???
HTML code
JS code
ILM
2
that’s a regular expression with a g (global) flag, the flags go after the closing / of the regular expression
this is replace syntax, let’s look at the documentation:
https://devdocs.io/javascript/global_objects/string/replace
$& will add the matched string in the string to substitute