1

Say I have var string1 = "Hello" and string2 = "Hello" How can I compare these two and ignore the capitals and the punctuation in javascript?

  • 1
    I think you're looking for something like [edit distance](https://en.wikipedia.org/wiki/Edit_distance). – Ted Hopp Mar 01 '17 at 19:04

1 Answers1

2

Try this:

Use String.toLowerCase() to lowercase a string. To remove punctuation, see this post: How can I strip all punctuation from a string in JavaScript using regex?

Then compare with the === operator. For example:

var string1 = "Hello";
var string2 = "Hello";

string1 = string1.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
string2 = string2.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");

if (string1.toLowerCase() === string2.toLowerCase()) {
    // Do something
}
Community
  • 1
  • 1
Hum4n01d
  • 1,312
  • 3
  • 15
  • 30