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

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

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