Sometimes, we want to replace a regex substring match in JavaScript.
In this article, we’ll look at how to replace a regex substring match in JavaScript.
How to replace a regex substring match in JavaScript?
To replace a regex substring match in JavaScript, we can use the string replace method with a regex.
For instance, we write
let str = "asd-0.testing";
const regex = /\d/;
str = str.replace(regex, "1");
console.log(str);
to call str.replace with regex and 1 to replace the digits in str with 1.
We use \d to match a single digit in the string.
Conclusion
To replace a regex substring match in JavaScript, we can use the string replace method with a regex.