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

90
Visualizações
Split JS string based on multiple different RegExr conditions

I have a JS string, say:

var testString = "words here, svg_icon('Var-1') svg_icon('Var_2'), and even more words here";

I understand how to match the string based on a fixed value e.g. if I want to match on svg_icon:

var testString = "words here, svg_icon('Var-1','var_3') svg_icon('Var_2'), and even more words here";
var filterMatch = /(svg_icon)/;
var results = testString.match(filterMatch);

results.forEach((result) => {
  console.log(result);
})

This logs the 2 matches it finds, which I'm expecting:

"svg_icon"
"svg_icon"

What I'm trying to do is match the expression so my output will be:

"svg_icon('Var-1','var_3')"
"svg_icon('Var_2')"

I think this is possible with Regular expressions, but cannot find how to deal with combining conditions whilst matching the changing "variable" names (e.g.'Var-1','var_3' and Var_2) that may contain special characters (just dashes and underscores).

Any help on what RegEx I can use in this situation, or if this is in fact possible with so many conditions.

Thanks in advance for any advice!

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

0

Match the ( through ) after svg_icon.

You need to use the g modifier to match multiple times. You're just matching the first svg_icon -- you're getting two outputs because match[0] is the full match, and match[1] is the capture group.

var testString = "words here, svg_icon('Var-1','var_3') svg_icon('Var_2'), and even more words here";
var filterMatch = /svg_icon\([^)]*\)/g;
var results = testString.match(filterMatch);

results.forEach((result) => {
  console.log(result);
})

about 4 years ago · Juan Pablo Isaza Relatório

0

Is this what you're looking for?

svg_icon\('[vV][aA][rR][\-_]\d+'(?:,'[vV][aA][rR][\-_]\d+')*\)

https://regex101.com/r/WctF2Q/1

about 4 years ago · Juan Pablo Isaza Relatório

0

Change regex to /svg_icon.+?\)/g

https://regex101.com/r/3zBfDq/1

Segment Meaning
svg_icon Match literal "svg_icon"...
. ...then anything but a new line...
+ ...for one to an unlimited amount of occurrences...
?\) ...until the first closing parenthesis (escaped with \).

Add a global flag which is required to use .matchAll() (.match() on steroids):

var testString = "words here, svg_icon('Var-1','var_3') svg_icon('Var_2'), and even more words here";
var filterMatch = /svg_icon.+?\)/g;
var result = [...testString.matchAll(filterMatch)].flat();
console.log(`Result is an array of matches:`);
console.log(result);
console.log(`Access a single match like so:`);
console.log(`First match is: result[0]`);
console.log(result[0]);
console.log(`Second match is: result[1]`)
console.log(result[1]);
.as-console-row::after { width: 0; font-size: 0; }
.as-console-row-code { width: 100%; word-break: break-word; }
.as-console-wrapper { min-height: 100% !important; min-width: 100%; }

The signature is just like .match() but you'll need to wrap everything into square brackets [] and use the spread operator ... and then flatten it with .flat(). The result is an array of matches.

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