如何测试字符串类型是否等于另一个 javascript [英] How do I test if a string kind of equals another javascript

查看:26
本文介绍了如何测试字符串类型是否等于另一个 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 var string1 = "Hello"string2 = "Hello"如何比较这两者并忽略 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?

推荐答案

试试这个:

使用 String.toLowerCase() 将字符串小写.要删除标点符号,请参阅此帖子:如何使用正则表达式从 JavaScript 中的字符串中删除所有标点符号?

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
}

这篇关于如何测试字符串类型是否等于另一个 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆