Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda