Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
JS Regex for Matrix Equations

I am writing a matrix calculator, in which the user enters a number of matrices and then writes an equation including the desired matrix. To verify the equation, I want a regex that returns 'true' if a character is not from the group [a-z , 0-9 , . , + , - , * , ^ , ( , ) ] is found. I've created an array of strings to test this regex, and it works with the exception of the final string. The strange this is that I've tested this on regex101, and on that website, the regex works as planned. How can I modify my regex in order for the last string to return 'true' as well as the other strings that returned 'true'?

let invalidCharRGX = /[^a-z0-9\.\+\-\*\^\(\)]/gi;
let strArr = [
  "A+B-C", //1 should return false
  "(A+B)-C", //2 should return false
  "A^+B^-C", //3 should return false
  "A*B-C", //4 should return false
  "2.5A+C", // 5 should return false
  "A=B", //6 should return true
  "[A-B]", //7 should return true
  "A&B", //8 should return true
  "A\B" //9 should return true
];
strArr.forEach((word, index) => {
    console.log(invalidCharRGX.test(word), 
                word.match(invalidCharRGX), 
                index + 1
               );
})

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The "A\B" is stripped of it escape when JS interpolates the string.
Since there is no escape-B special character, the escape is stripped off
in the initial language parsing.

let invalidCharRGX = /[^a-z0-9\.\+\-\*\^\(\)]/gi;
let strArr = [
  "A+B-C", //1 should return false
  "(A+B)-C", //2 should return false
  "A^+B^-C", //3 should return false
  "A*B-C", //4 should return false
  "2.5A+C", // 5 should return false
  "A=B", //6 should return true
  "[A-B]", //7 should return true
  "A&B", //8 should return true
  "A\\B", //9 should return true
  "A\B" //10 should return false
];
strArr.forEach((word, index) => {
    console.log(word,
                invalidCharRGX.test(word), 
                word.match(invalidCharRGX), 
                index + 1
               );
})

about 4 years ago · Juan Pablo Isaza Denunciar

0

This isnt regex problem, its the way you have defined the string in your last entry i.e "A\B", you need to escape slash with another one i.e "A\\B", otherwise its ignored.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda