I'm trying to test a little regex pattern on a string array. When I use the pattern directly with test function, it works correctly. But when I use the pattern as a constant variable, it doesn't work anymore.
Can someone explain what's wrong with my code ? Or how can I correct this ?
Thanks :)
const strArray = ['(', 'ATT1', 'VARCHAR2', ')'];
const testingWord = (pString: string) => /^[^;() ]+$/g.test(pString);
strArray.map((word) => {
console.log(word, testingWord(word));
});
// RESULT
// ( false
// ATT1 true
// VARCHAR2 true
// ) false
const PATTERN_WORD = /^[^;() ]+$/g;
const test = (pString: string) => PATTERN_WORD.test(pString);
strArray.map((word) => {
console.log(word, testingWord(word));
});
// RESULT
// ( false
// ATT1 true
// VARCHAR2 false <-- this should be true
// ) false