I want to wrap all occurrences of certain words in a given string with square brackets in JavaScript.
Say these words are apples, oranges, and bananas. Then a subject text "You are comparing apples to oranges." should turn into "You are comparing [apples] to [oranges]."
The regular expression for this would be (apples|oranges), but the question is how to wrap or more generally, modify each match. String.replace() lets you replace matches founds with some pre-defined value, rather a value based on the match.
Thanks.
(apples|oranges)...