Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda