var name = "dea+hlrd"; var text = "This dea+hlrd guy is super annoying."; var escaped = name.replace(/[^\w\s]/g, "\\$&"); var regexp = new RegExp("\\b(" + escaped + ")\\b", "gi"); console.log(text.replace(regexp, "_$1_")); // → This _dea+hlrd_ guy is super annoying.
I've played around with the var escape, and I noticed that the outcome is still the same when I remove \s. Why is that? If this is the case, why do we need both?
When I console.log(escaped), it results in
dea\+hl\[\]rd. How did this happen from \$&?
I am completely unable to understand var regexp.
I thank you for taking your time to read this.
I appreciate all help provided.
Submitted July 15, 2017 at 06:10PM by NewbiusCoder
via reddit http://ift.tt/2uvJbVc