How Do I Test If A String Kind Of Equals Another Javascript
Say I have var string1 = 'Hello' and string2 = 'Hello' How can I compare these two and ignore the capitals and the punctuation in javascript?
Solution 1:
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
}
Post a Comment for "How Do I Test If A String Kind Of Equals Another Javascript"