Employing JavaScript, I am currently searching a body of text. Users are given the option to input any string they desire, and then I aim to search for that specific string, ensuring it is considered a "whole word" located between boundaries.
All I need is the ability to execute something like:
var userString = "something blah";
// => "blah another thing blah"
"blah something blah blah".replace(new RegExp("\\b" + userString + "\\b"), "another thing");
// no match, which is great
"blahsomething blah blah".replace(new RegExp("\\b" + userString + "\\b"), "another thing");
userString = "something\\blah";
// should be a match, but regrettably isn't
"blah something\\blah blah".replace(new RegExp("\\b" + userString + "\\b"), "another thing");
The challenge arises with special characters - so I am in search of a method to instruct the RegExp to handle the user's input as a literal or to reserve a portion of the expression as such. Is this achievable within JavaScript?