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

251
Vistas
How can I compare two strings ignoring new line characters, white spaces, non-breaking space?

I need to compare two strings ignoring white spaces, newline characters, non-breaking space. Strings should be equal.

moves the cursor down to \r\n \r\n the next line without returning.

moves the cursor down the next          \r\n \r\n the line without returning.

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

0

You can get rid of them with a simple regex.

const pureString = str.replace(/(?:\r\n|\r|\n)/g, '');

And then do your comparison between the two string.

In the regex you are replacing all the \r\n, \r and \n in your string with '', so with nothing.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could strip the two strings of all the characters you want to ignore.

To strip a string of all the characters you don't want you can use the replace() function. By using \s we remove all spaces, tabs and new-lines, if you want something more specific you have to change the first argument of the replace() function.

string.replace(/\s+/g, '')

(see this SO-question to understand how it works: Remove whitespaces inside a string in javascript)

To compare two strings you could use

string1.localeCompare(string2)

(see this SO-question to understand how it works: Optimum way to compare strings in JavaScript? )

If you put these two concepts together, you get

string1 = string1.replace(/\s+/g, '')
string2 = string2.replace(/\s+/g, '')
string1.localeCompare(string2)

which should answer your question.

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