javascript - how do i escape a variable in this regex -
please see following: https://regex101.com/r/gg3eo8/1
what want instead of using constant string, in case 'i'm h1', want insert variable there value dynamic, following tried guess i'm not escaping properly
var value = "random string"; "/<h1>.*?" + value + ".*?<\/h1>/g"
what right way escape above?
use regexp
constructor.
if (!regexp.escape) { regexp.escape = function (value) { return value.replace(/\w/g, "\\$&") }; } var reg = new regexp("<h1>.*?" + regexp.escape(value) + ".*?</h1>", "g");
Comments
Post a Comment