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

218
Vistas
Substrings or split methods in javascript

I have a simple problem that I think I need help with. So, I have a function that accepts a string in this format

"1. Crowe, Velvet (LoC), 2. Hume, Eleanor (Ext), 4. Shigure, Rokurou (DmN), 10. Mayvin, Magilou (MgC)" without the quotation marks.

Basically, a list of person's names with rank number.

What I want is to split them such that I will get the ff result:

[
   "1. Crowe, Velvet (LoC)", 
   "2. Hume, Eleanor (Ext)", 
   "4. Shigure, Rokurou (DmN)",
   "10. Mayvin, Magilou (MgC)"
]

Is there a way to do that? I used split() method but it splits the string every time it sees an occurrence of the comma.

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

0

You could split using a regular expression /(?<=\)),\s/:

const str = "1. Crowe, Velvet (LoC), 2. Hume, Eleanor (Ext), 4. Shigure, Rokurou (DmN), 10. Mayvin, Magilou (MgC)";
const res = str.split(/(?<=\)),\s/);
console.log(res);

This will split on ), as suggested by cmgchess in the comments. The issue with splitting based on a string is that it removes some of the characters you want to keep. Instead, by using a regular expression, you can use (?<=\)) (here ?<= is called a positive lookbehind) to match and keep the ) in the resulting split element. The ,\s then splits based on a comma followed by a space (\s).

You could also use the following regex with .match() which is a little more robust, but may need to be updated depending on the text that can come after your number/rank:

/\d+\.\s\w+,\s\w+\s\(\w+\)/g

The above performs:

  • \d+\.: Will match one or more (+) digits followed by a dot
  • \s: Match a whitespace character
  • \w+,: Match one of more (+) word-characters (\w) followed by a comma ,
  • \s: Matches a space
  • \(\w+\): Match word characters surrounded by parentheses ( )
  • /g: Global flag to match the sequences for all occurrences in the string

const str = "1. Crowe, Velvet (LoC), 2. Hume, Eleanor (Ext), 4. Shigure, Rokurou (DmN), 10. Mayvin, Magilou (MgC)";
const res = str.match(/\d+\.\s\w+,\s\w+\s\(\w+\)/g);
console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Just a little improvement of @cmgchess's answer (add trim method to remove unnecessary spaces):

const getUsers = (str) => {
    const users = str.split('),').map(x => x.trim() + ')');
    users[users.length - 1] = users[users.length - 1].replace('))', ')');

    return users;
}

console.log(getUsers("1. Crowe, Velvet (LoC), 2. Hume, Eleanor (Ext), 4. Shigure, Rokurou (DmN), 10. Mayvin, Magilou (MgC)"));

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