Testing alpha-numeric string with XRegExp giving incorrect results

27 views Asked by At

I have the following

regex-patterns.ts

import { match } from 'ts-pattern'
import XRegExp from 'xregexp'


export const alphanumRegex = (action = 'validate'): RegExp | string => {
   const baseRegex = XRegExp.tag('gimx')`A-Z0-9`
   const finalRegex = XRegExp.tag(baseRegex.flags)`[${baseRegex}]+`
 
     return match(action)
         .returnType<RegExp | string>()
         .with('negate', () => negateRegex(baseRegex))
         .with('validate', () => inputValidationRegex(finalRegex))
         .otherwise(() => absentParamError(action, alphanumRegex.name))
   
}

export const absentParamError = (action: string, fnName: string) =>
   `Absent identifier '${action}' for '${fnName}()'`

export const inputValidationRegex = (regex: RegExp) =>
   XRegExp.tag(regex.flags)`^(${regex})$`


export const negateRegex = (regex: RegExp) =>
   XRegExp.tag(regex.flags)`[^${regex}]*`


// Testing the regex

     const retVal1 = XRegExp.test('abc123', alphanumRegex('validate') )
     const retVal2 = XRegExp.test('abc123', /^([A-Z0-9]+)$/gmi)

console.log( {retVal, retVal1, retVal2}) // {retVal: retVal1: false, retVal2: true}


To me it seems that retVal1 and retVal2 RegExp should be identical, but the results are different.

Where is my mistake?

Thanks

0

There are 0 answers