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

136
Vistas
Substring Stack Split - Regex

I'm trying to split a string into a stack/array of string based on a regex match. The string can have Remix Icon embedded in it, that I'll extract and render HTML attributes based on the positioning.

Objective

The icon names will be wrapped by : before and after. The objective is to split strings like so:

  • "Select an option:ri-arrow-down-s-line:" ---> ["Select an option", ":ri-arrow-down-s-line:"]
  • "Download :ri-download-line: or upload :ri-upload-line:" ---> ["Download ", ":ri-download-line:", " or upload ", ":ri-upload-line:"]
  • ":ri-download-line:" ---> [":ri-download-line:"]

What's happening

I tried using this.label.split(/:ri-.*:/) but it doesn't include the icon names in the result stack and doesn't work with repetitions. Something like "Download :ri-download-line: or upload :ri-upload-line:" gives an output of ['Download ', ''] with this.

  • Improvement 1: Using /:ri-[^:]*:/ improves the string splitting, and accounts for repetitions. However now I need to figure out how to include the matched regex in the stack at the respective positions

Can someone please point me to the right direction?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use this simple refex to do the splitting:

/(?=:ri)/g

It simply splits whenever there's :ri to the right.

Note it uses the global flag so it can match and split on all occurences.

Now you have an array. If you want the syntax shown in your question, you can use JSON.stringify on the result.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You might use capturing group in split /(:ri-[^:]+:)/ to be included into the result, then you might have to filter empty elements from the split result:

const Split = s => s.split(/(:ri-[^:]+:)/).filter(Boolean);

console.log(Split("Select an option:ri-arrow-down-s-line:"))
console.log(Split("Download :ri-download-line: or upload :ri-upload-line:"))
console.log(Split(":ri-download-line:"))

about 4 years ago · Juan Pablo Isaza Denunciar

0

Instead of splitting on 1, it could be matching both.

For example:

const label = "Download :ri-download-line: or upload :ri-upload-line:"

let arr = label.match(/(:ri-[^:]*:)|[^:]+/g);

console.log(arr);

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