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

128
Vistas
Split string by placeholder with optional prepended string

I'm trying to create a regex to match a string, which has a * placeholder. The problem for me is, that this placeholder could optionally have some characters in front:

I came up with /(.*)(\w)\*(.*)/:

This is ju* an example
This is * an example
T* is just an example
* is just an example
This is just an ex*
This is just an *

I need to get three parts of each string:

  1. The first part of the string before the placeholder (with optional prepended string),
  2. the prepended placeholder string itself and
  3. the rest of the string.

So the result should be:

This is|ju|an example
This is|<null>|an example
<null>|T|is just an example
<null>|<null>|is just an example
This is just an|ex|<null>
This is just an|<null>|<null>    

https://regex101.com/r/0pFhdE/1

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

0

What I tried is:

^(.*?)[^\S\n]*([^\s*]*)\*[^\S\n]*(.*)$

See an online demo


  • ^ - Start-line anchor;
  • (.*?) - Your 1st capture group with 0+ (Lazy) characters other than newline;
  • [^\S\n]* - 0+ (Greedy) character that are not non-whitespace characters or newline. I guess you may swap to \s* in your application, but for demonstration purposes I used this;
  • ([^\s*]*) - A 2nd capture group to match 0+ (Greedy) characters other than whitespace or asterisks;
  • \*[^\S\n]* - A literal asterisks, followed by 0+ (Greedy) characters that are not non-whitespace or newline;
  • (.*) - A 3rd capture group to catch 0+ (Greedy) characters other than newline;
  • $ - End-line anchor.
about 4 years ago · Juan Pablo Isaza Denunciar

0

This seems to match your requirements.

/^(.*?)(\w*)\*(.*)/g

const arr = [
  "This is ju* an example",
  "This is * an example",
  "T* is just an example",
  "* is just an example",
  "This is just an ex*",
  "This is just an *",
];

console.log(
  arr.map(x => {
    const myRegexp = /^(.*?)(\w*)\*(.*)/g; // new RegExp("", "gm");
    const match = myRegexp.exec(x);
    return match.map(x => x.trim());
  })
);
.as-console-wrapper {
  max-height: 100vh !important;
}

Reference: greedy and lazy matching

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use yourString.split(/(\w**)/)

the responses will be:

[ "This is ", "ju*", " an example" ]

[ "This is ", "*", " an example" ]

[ "", "T*", " is just an example" ]

[ "", "*", " is just an example" ]

[ "This is just an ", "ex*", "" ]

[ "This is just an ", "*", "" ]

handle the blanks and * to match your outcome

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