Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

compare chars of two strings and ignore white space between words

I have 2 strings that use the same letters but one of them contains empty spaces at the end and between words

const message = 'Hello javascript world ';
const message1 = 'Hello     javascript world              ' ;

i want to ignore these empty spaces from 2 strings to only compare strings by the rest of chars in order to get a boolean result equal to true when i do so message === message1

imo this need a regex

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, you can use str.replace(/\s+/g, '') to remove the spaces then simply compare the strings
That would look something like this:

const message = 'Hello javascript world ';
const message1 = 'Hello     javascript world              ' ;

const equal = message.replace(/\s+/g, '') == message1.replace(/\s+/g, '');
// true
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading