JS: RegExp Constructor
new RegExp(args)-
same as
RegExp(args) RegExp(str, flagsStr)-
Create a RegExp Object from string.
console.log("aa".replace(RegExp("a", "g"), "b") === "bb"); // true 🟢 TIP: be sure to use double backslash in string. 〔see JS: String Escape Sequence〕
// replace digits by x. console.log("8314".replace(RegExp("\\d+"), "x") === "x"); // true
When Arg is regex object
if the first arg is using slash syntax, it is already a regex object, still works:
// the argument /a/g is already a regex object console.log("aa".replace(RegExp(/a/g), "b") === "bb"); // true // above is same as console.log("aa".replace(RegExp(RegExp("a", "g"), "g"), "b") === "bb"); // true
JavaScript. Regular Expression
- JS: Regular Expression Tutorial
- JS: Regular Expression Functions
- JS: Create Regex Object
- JS: Regular Expression Syntax
- JS: Regular Expression Flags
- JS: Regex Replace String Dollar Sign
- JS: Regex Replace Function Args
- JS: RegExp (class)
- JS: RegExp Constructor
- JS: RegExp.prototype
- JS: String.prototype.search
- JS: String.prototype.match
- JS: String.prototype.matchAll
- JS: String.prototype.replace
- JS: String.prototype.replaceAll
- JS: RegExp.prototype.test
- JS: RegExp.prototype.exec