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

119
Visualizações
JavaScript Regex finding all substrings that matches with specific starting and ending pattern

I want a Javascript regex or with any possible solution, For a given string finds all the substrings that start with a particular string and end with a particular character. The returned set of subStrings can be an Array.

this string can also have nested within parenthesis.

var str = "myfunc(1,2) and myfunc(3,4) or (myfunc(5,6) and func(7,8))";

starting char = "myfunc" ending char = ")" . here ending character should be first matching closing paranthesis.

output: function with arguments.

[myfunc(1,2),
 myfunc(3,4),
 myfunc(5,6),
 func(7,8)]

I have tried with this. but, its returning null always.

var str = "myfunc(1,2) and myfunc(3,4) or (myfunc(5,6) and func(7,8))";

var re = /\myfunc.*?\)/ig
var match;
while ((match = re.exec(str)) != null){
  console.log(match);
}

Can you help here?

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

0

I tested your regex and it seems to work fine:

let input = "myfunc(1,2) and myfunc(3,4) or (myfunc(5,6) and func(7,8))"

let pattern = /myfunc.*?\)/ig 
// there is no need to use \m since it does nothing, and NO you dont need it even if you use 'm' at the beginning.

console.log(input.match(pattern)) 
//[ "myfunc(1,2)", "myfunc(3,4)", "myfunc(5,6)" ]

If you use (?:my|)func\(.+?\) you will be able to catch 'func(7,8)' too.

(?:my|)

(    start of group
?:   non capturing group
my|  matches either 'my' or null, this will match either myfunc or func
)    end of group

Test the regex here: https://regex101.com/r/3ujbdA/1

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