Hi there and thank you for your help in advance.
I’m looking for some help with using a variable to dynamically replace a name in some text. I’m really stuck with this and would appreciate any help.
So far i’ve tried:
var name = 'Page2of4Sandor-0607231529Tyrell'
var t = "Sandor"
var prt1 = "/Page.of." + t + "gi"
var reggy = new RegExp(prt1)
console.log(name.replace(reggy, ''))
This seemed like sensible approach as
.replace(/Page.of.Sandor/gi, '')
Works just fine.
Any help appreciated.
2
Answers
Spent a little more time on and found this to work:
You could create a function (factory) that initializes a regular expression and returns a reusable function.
Here is it is thunk form. This can also be referred to as "currying".