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

229
Visualizações
Regex to get 2 specific characters between other 2 specific characters

I have a string:

[](first-(second))

I would like to remove ( and ) between [](... and last ...)

With that being said, I'm currently using (?<=\[\]\()(.*?)(?=\))

I assume (.*?) needs to be replaced with some sort of expression that will find ( and ) in between.

Something similar to string.replace(/(?<=\[\]\()(.*?)(?=\))/g, '')

And the string should look like [](first-second)

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

0

As you already use a lookbehind in the pattern with JavaScript, you could use an infinite quantifier in the lookbehind, match the 2 parenthesis that you don't want to keep and capture in group 1 what you want to keep.

In the replacement use group 1 using $1

(?<=\[\]\([^()]*)\(([^()]*\))\)

The pattern matches:

  • (?<= Positive lookbehind, assert what is to the left is:
    • \[] Match []
    • \([^()]* Match ( and 0+ times any char except ( and )
  • ) Close lookbehind
  • \( Match (
  • ([^()]*\)) Capture group 1, match 0+ times any char except ( and )
  • \) Match )

Regex demo

const s = "[](first-(second))"
const regex = /(?<=\[]\([^()]*)\(([^()]*\))\)/;
console.log(s.replace(regex, "$1"))

Or using 2 capture groups instead of a lookbehind:

(\[]\([^()]*)\(([^()]*\))\)

Regex demo

const s = "[](first-(second))"
const regex = /(\[]\([^()]*)\(([^()]*\))\)/;
console.log(s.replace(regex, "$1$2"))

about 4 years ago · Juan Pablo Isaza Relatório

0

If you don't need a stylish code, why not to go the easy way? Extract the substring without external brackets, replace & concat. the removed chars

s = '[](first-(second))'

mySubstring = s.substring(3, s.length -2);  // 'first-(second)'
//your replacement func.
result = '[]('+mySubstring+')';             //'[](first-second)'
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is an example (not using lookaheads). It should steer you in the correct direction.

Using 2 capture groups, and some non-greedy sets, you can assemble your target string.

It assumes no additional nested parenthesis or brackets, although you can modify to your needs to only include alphanumeric characters in the character sets.

const d = '[](foo-(bar)) [](foo2-(bar2))'
const rx = /(\[\]\([^-\(\)\[\]]*?-)\(([^\)]*?)\)\)/g

const m = rx.exec(d)
console.log(m)

const fixed = d.replace(rx, '$1$2)')
console.log(fixed)

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