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

197
Visualizações
How do I split a value using the split function from javascript to split checking only the first and last occurence

So I need to split an regex that's coming from my database. The value is like the following

regexValue = '/^([0-9]{3}\.?[0-9]{3}\.?[0-9]{3}\-?[0-9]{2}|[0-9]{2}\.?[0-9]{3}\.?[0-9]{3}\/?[0-9]{4}\-?[0-9]{2})$/ig'

if you analyze throughly this regex you can see that it have an forward slash '/' between the regex. So if I wanted to split it, using

values.split('/'); 

It would generate more strings than I would like to. For example a simple regex

//would generate three values("", \w+ , ig)    
regexValue = '/\w+/ig'
regexValue.split('/');

Is there a way so I could only split getting the first and last values?

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

0

If you want to split that string into the regular expression bodyand its flags, you can do that easily with...a regular expression. 😃

const parts = /^\s*\/(.*)\/(?:([a-z]*))\s*$/.exec(regexString);
//           A ^
//           B  ^^^
//           C     ^^
//           D       ^^^^
//           E           ^^
//           F             ^^^^^^^^^^^^
//           G                         ^^^
//           H                            ^
  • A: Start of input
  • B: Optional whitespace
  • C: Leading slash of regex "literal"
  • D: Body of the expression
  • E: Ending slash of regex "literal"
  • F: Optional flags
  • G: Optional whitespace
  • H: End of input

Live Example:

const regexString = '/^([0-9]{3}\\.?[0-9]{3}\\.?[0-9]{3}\\-?[0-9]{2}|[0-9]{2}\\.?[0-9]{3}\\.?[0-9]{3}\\/?[0-9]{4}\\-?[0-9]{2})$/ig';

const parts = /^\s*\/(.*)\/(?:([a-z]*))\s*$/.exec(regexString);
if (!parts) {
    console.log("No match");
} else {
    const [, body, flags] = parts;
    console.log(`body:  ${body}`);
    console.log(`flags: ${flags}`);
}


Note that I escaped the backslashes in the string literal, since you said you're reading this from your database, you don't have it in a string literal in your code. It's just that inside a string literal, a backslash is an escape character, so \. and . mean exactly the same thing. To write a string literal defining a string with backslashes in it, you have to escape them as I've done in the snippet above. Again, you've said it comes from your database, so you're fine, but I figured I should escape them in the example to avoid being misleading.

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