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

95
Vistas
Split string matching a pattern JavaScript

I have a string like this:

"First player vs. Second player     Percentage%"

First player and Second player can be multiple words and before the Percentage% I'm using three  . How can I split this string to have an array containing First player and Second player (Percentage is not necessary)?

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

0

This should do what you want:

const data=["First player vs. Second player     Percentage%",
            "Another first player vs. a second one      Percentage%",
            "One     vs. Two      Two Percentage%"];
            
const res = data.map(s=>s.match(/(.+?)\s+vs\.\s+(.+?)\s*\ \ \ /).slice(1));
console.log(res)

In the regexp I am looking for two matching groups (.+?). These are non-greedy matches, as they are followed or preceded by \s+ (one or more white space characters) and I don't want these whitespace characters in my matching groups.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Something like this should do it

const test1 = 'First player vs. Second player     Percentage&emsp'
const test2 = 'Ryu vs. Ken     100&emsp'

const extractPlayers= (string) => {
  const [firstPlayer, rest] = string.split(' vs. ')
  const secondPlayer = rest.split(' ').filter(x => x).slice(0, -1).join(' ').trim()
  
  return {firstPlayer, secondPlayer}
}

console.log(extractPlayers(test1))
console.log(extractPlayers(test2))

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