'Node JS Character of of Range
I have a regular expression that I'd like to store in a variable. The expression works file when used directly in the file reader but when expressed as a constant, I get a range out of order error.
Here is the expression:
let regex = "/([a-zA-Z0-9\s_\\.\-\(\):])+(.pdf)/i";
// The file can be read below
fs.readdirSync('path').filter((fp) =>
fp.match(/([a-zA-Z0-9\s_\\.\-\(\):])+(.pdf)/i) !== null);
//The file cannot be read here when using the constant
fs.readdirSync(WATCHED_DIR).filter((fp) =>
fp.match(regex)/i) !== null)
If I do this i.e. without the quotes, I get a syntax error.
let regex = /([a-zA-Z0-9\s_\\.\-\(\):])+(.pdf)/i;
SyntaxError: Invalid regular expression: /[a-zA-Z0-9\s_\.-():])+(.pdf)/: Unmatched ')'
How can I correctly store the regular expression in a constant?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
