var str = "^" + "/post/\d+" + "$";
var regex = new RegExp(str);
var flag = regex.test("/post/3333");
console.log(flag) // -> false
console.log(regex) // -> /^\/post\/d+$/
I'm expecting the result becomes true, but it results in false.
I think the problem is "\" is added automatically before "/" when RegExp instance is created.
How am I supposed to write in order to make it work?


RegExpconstructor, you need to escape the backslashes twice. Once for the string and next for the regex. Use"^" + "/post/\\d+"regex--good debugging technique! Once you've done that, the problem is staring you in the face. You will see it says/^\/post\/d+$/. Note that the slashes have been transformed into\/as they should, but thedhas no preceding backslash! So it will just match an actuald! This would have been a great clue for you to start understanding what is happening.