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

80
Visualizações
Regex match all excluding a delimiter

I have two sample strings of code like so:

"const x = anything;"
"const {a, b, c, d} = anything;"

I want to match x, as well as a, b, c, and d (none of which are literal, they can be any valid JavaScript variable character(s), but are delimited by commas) if they are preceded by the keyword const. The latter is the destructuring syntax.

Context: this is to extend syntax highlighting of code tokens in PrismJS.

What I've tried so far:

  Prism.languages.insertBefore(lang, 'operator', {
    constant: {
      pattern: /(const\s?{?)[\s_$a-zA-Z\xA0-\uFFFF,]*(?!:)/,
      lookbehind: true,
    },
  });

The comma is inside the [], and I am not sure of how to "continue" matching each variable inside the braces without including the comma character.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this pattern:

(?:const\s+{?\s*)[_$\w]+(?:[,}\s])

I'll try and improve it by replacing \w to match identifiers using non-alphanumeric characters. Possibly:

(?:const\s+{?\s*)[_$\p{L}]+(?:[,}\s])
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