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

114
Vistas
Regular expression for cleaning up strings

[UPDATED]

In an effort to clean up an array of strings that contains people names as well as other titles or user IDs (if present), I need to remove any non-letter characters as well as all characters that are surrounded by parentheses, quotations marks, brackets etc.

For example ["Sean Jackson(he/him)", "Mack $ Adams"] needs to be changed to ["Sean Jackson", "Mack Adams"].

To achieve this I need to write a regular expression that can match such instances but unfortunately my regular expression does not work as intended.

let names = [
  'Sean Jackson (he/him)',
  'Steven Robinson',
  'Mack $ Adams',
  'Keira (12345) Nightly',
 ];

for (i in names) {
  names[i] = names[i].replace(/\W*\W/g, '');
 }

console.log(names);

The above code returns:

["SeanJacksonhehim", "StevenRobinson", "MackAdams", "Keira12345Nightly"]

Whereas what I want is:

["Sean Jackson", "Steven Robinson", "Mack Adams", "Keira Nightly"]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

So after some digging and with helpful comments from the community I found the following working solution.

Since a space is counted as a non-alphanumerical character itself (\s is captured by non-alphanumerical regular expression \W), I had to specify that two consecutive non-alphanumerics need to be on the left side of the target string (hence using \W{2}).

And then on the right side of the target string there should be any number of characters except for space \S*.

let names = [
  'Sean Jackson (he/him)',
  'Steven Robinson',
  'Mack $ Adams',
  'Keira (12345) Nightly'
 ];

for (i in names) {
  names[i] = names[i].replace(/\W{2}\S*/g, ''); //removes the following patter: [any two non-alphanumerical character followed by any number of any characters except for space]
}
console.log(names);

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