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

168
Vistas
How to match everything outside of brackets and everything inside of brackets?

These are some demo strings,

enum('a', 'b', 'c')
varchar(255)
int unsigned

Taking first one as an example, I want to match enum and 'a', 'b', 'c'. Everything inside brackets are optional as you see in 3rd example: int unsigned. How do I do this?

I tried,

(.*)(\((.*)\))?

However this matches whole string for some reason.

const out = /(.*)(\((.*)\))?/.exec(`enum('a', 'b', 'c')`)
console.log(out)

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

0

You can use

const [, first, second] = /^(.*?)(?:\((.*)\))?$/.exec(`enum('a', 'b', 'c')`)
console.log(first + '\n' + second)

See the regex demo. Details:

  • ^ - start of string
  • (.*?) - Group 1: any zero or more chars other than line break chars as few as possible
  • (?:\((.*)\))? - an optional sequence of (, any zero or more chars other than line break chars as many as possible (captured into Group 2), and then a ) char
  • $ - end of string.
about 4 years ago · Juan Pablo Isaza Denunciar

0

const tests = [
    "enum('a', 'b', 'c')",
    "varchar(255)",
    "int unsigned"
];

tests.forEach(test => console.log(/([^(]+)(?:\((.*)\))?/.exec(test)));

The first capturing group grabs the part before the parenthesis, the second optionally what is contained within the parenthesis if applicable.

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