I am not able to create a regex from a variable, using template literals
What is wrong and how to fix it?
const myValue = 'a.b'
const reg = new RegExp(`/^${myValue}$/`);
/*
/^a.b/
*/
I am not able to create a regex from a variable, using template literals
What is wrong and how to fix it?
const myValue = 'a.b'
const reg = new RegExp(`/^${myValue}$/`);
/*
/^a.b/
*/
Remove the slashes from the template literal. Slashes inside the string are escaped by the constructor, and included as part of the pattern.
const myValue = 'a.b'
const reg = new RegExp(`^${myValue}$`);
/*
/^a.b$/
*/
console.log(reg);