Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!