Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different RegExp rules between RE2 for node and RE2JS #14

Open
Cyberxon opened this issue Jan 31, 2025 · 1 comment
Open

Different RegExp rules between RE2 for node and RE2JS #14

Cyberxon opened this issue Jan 31, 2025 · 1 comment

Comments

@Cyberxon
Copy link

Hello,

At our solution, We want to use the RE2 engine in our system and we wanted to validate the regexes both on the frontend side (React) and in the backend (node). for the backend we were thinking of using RE2 npm package and for the frontend we want to use RE2JS.

Sadly, when we were trying to run some regexes that errored out with the RE2 package we noticed that RE2JS accepted them.

the code used to compare the regexes:

import csv from 'csv-parser'
import RE2 from 're2'
import { RE2JS } from 're2js'

const inputCsv: string = 'invalid_regexes.csv' // Replace with your actual CSV file path
const outputCsv: string = 're2_vs_re2js.csv'

interface RegexResult {
 pattern: string
 flags: string
 re2Error: string
 re2jsError: string
}

const results: RegexResult[] = []

function testRegex(engine: string, pattern: string): string {
 try {
   if (engine === 'RE2') {
     // eslint-disable-next-line no-new
     new RE2(pattern)
   } else if (engine === 'RE2JS') {
     RE2JS.compile(pattern)
   }
   return 'Valid'
 } catch (error) {
   return (error as Error).message ?? ''
 }
}

fs.createReadStream(inputCsv)
 .pipe(csv())
 .on('data', (row: any) => {
   const regexData = JSON.parse(row['All invalid regexs'])
   const pattern: string = regexData.pattern
   const flags: string = regexData.flags || ''
   const re2Error: string = testRegex('RE2', pattern)
   const re2jsError: string = testRegex('RE2JS', pattern)

   if (re2Error === 'Valid' || re2jsError === 'Valid') {
     results.push({ pattern, flags, re2Error, re2jsError })
   }
 })
 .on('end', () => {
   const outputStream = fs.createWriteStream(outputCsv)
   outputStream.write('Pattern,Flags,RE2_Error,RE2JS_Error\n')

   results.forEach(({ pattern, flags, re2Error, re2jsError }) => {
     outputStream.write(`"${pattern}","${flags}","${re2Error}","${re2jsError}"\n`)
   })

   console.log(`Validation complete! Results saved to ${outputCsv}`)
 })

The resulting csv file is this one:

re2_vs_re2js.csv

Why is there this difference between the two packages for the same engine RE2?
I realize that both packages are not being maintained by the same people but is there a way to have the same rules for both frontend and backend packages?

Thank you in advance and keep up the good work!

@le0pard
Copy link
Owner

le0pard commented Jan 31, 2025

This commit may fix your issues. Please check from main branch. You can use yarn build - it will build you RE2JS files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants