I have an issue with dynamic regular expression. Here is the sample
var searchStr = "C:\\Users\\fds\\Desktop\\Node"
num = 2
var p ='([a-zA-Z]*:{1}(\\[a-zA-Z]*){'+num+'})'
var p1 = new RegExp(p,"gi")
console.log(p1)
let wabpath = searchStr.replace(p1,"")
console.log(wabpath)
I'm trying to pass the {num} dynamically but it's not working, Could anyone please help me on this?
You are using backslash in your regex. But they act as escape characters. If you want to match actual backslashes, you need to escape them as well. In your case, you need to espace two backslash, resulting in four in a row.
var searchStr = "C:\\Users\\fds\\Desktop\\Node"
num = 2
var p ='([a-zA-Z]*:{1}(\\\\[a-zA-Z]*){'+num+'})'
var p1 = new RegExp(p,"gi")
console.log(p1)
let wabpath = searchStr.replace(p1,"")
console.log(wabpath)