JS: RegExp Constructor
new RegExp(args)-
same as
RegExp(args)
RegExp(str, flagsStr)-
Create a RegExp Object from string.
console.assert("aa".replace(RegExp("a", "g"), "b") === "bb"); 🟢 TIP: be sure to use double backslash in string. 〔see JS: String Escape Sequence〕
// replace digits by x. console.assert("8314".replace(RegExp("\\d+"), "x") === "x");
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.assert("aa".replace(RegExp(/a/g), "b") === "bb"); // above is same as console.assert("aa".replace(RegExp(RegExp("a", "g"), "g"), "b") === "bb");
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