JS: RegExp Constructor
new RegExp(args)
-
same as
RegExp(args)
RegExp(str, flagsStr)
-
Create a RegExp Object from string.
See also: • RegExp Syntax • RegExp Flag
Examples
Using the slash syntax for pattern:
const xxStr = "about time"; const xxRegex = RegExp( /t/g ); console.log( xxStr.replace(xxRegex, "_" ) ); // abou_ _ime
Using the string syntax for pattern:
const xxStr = "about time"; const xxRegex = RegExp("t", "g"); console.log( xxStr.replace(xxRegex, "_" ) ); // abou_ _ime
Double Slash
Note: when using string to construct a pattern, you need to use double backslash for any single backslash in regex. Because, single backslash is used in string to escape the next character. So, when you use double backslash, the RegExp will just get a single backslash.
const xxStr = "30 thousand"; const xxRegex = RegExp("\\d+"); console.log( xxStr.replace(xxRegex, "_" ) ); // _ thousand
JavaScript, Regular Expression
- JS: RegExp Tutorial
- JS: Regex Functions
- JS: RegExp Syntax
- JS: RegExp Flag
- JS: Regex Replace String Dollar Sign
- JS: Regex Replace Function Args
- JS: RegExp Object
- 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