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

127
Vistas
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 Respuestas
Responde la pregunta

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 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