[英]How do I test if a string kind of equals another javascript
假设我有var string1 = "Hello"
和string2 = "Hello"
如何比较这两个变量,并忽略javascript中的大写字母和标点符号?
Try this: 尝试这个:
Use String.toLowerCase()
to lowercase a string. 使用
String.toLowerCase()
小写字符串。 To remove punctuation, see this post: How can I strip all punctuation from a string in JavaScript using regex? 要删除标点符号,请参阅这篇文章: 如何使用正则表达式从JavaScript中的字符串中删除所有标点符号?
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.