如何正确检查字符串是否不包含特定单词? [英] How can I correctly check if a string does NOT contain a specific word?

查看:108
本文介绍了如何正确检查字符串是否不包含特定单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试找出如何解决上述问题。
具体来说,我想检查字符串是否不包含大写字母和小写字母的流字。

I am currently trying to figure out how to solve the above named problem. Specifically I want to check if the string does not contain the word "stream" both in capital and lowercase letters.

这里是到目前为止我的代码:

Here's my code so far:

if (((gewaesser_name1.includes("Stream") == "false") ||
(gewaesser_name1.includes("stream") == "false")) &&
((gewaesser_name2.includes("Stream") == "false") ||
(gewaesser_name2.includes("stream") == "false")))
{var a= "..."}

代码显然不起作用,因为结果不是我期望的那样。
在使用以下语法变体之前,我还尝试过使用 indexOf 方法:

The code does obviously not work as the results are not what I expect them to be. I also tried to use the indexOf method before using the following syntax variations:

gewaesser_name2.indexOf("stream") == -1
gewaesser_name2.indexOf("stream") < 0

这些变化似乎都不适合我。谁能给我一个提示,这是什么问题?我曾经多次使用 indexOf 方法,但总是在我想检查字符串是否确实包含特定单词而不是相反时使用。

None of these variations seem to work for me. Could anyone please give me a hint what's the problem here? I used the indexOf method before many times but always when I wanted to check if a string did contain a specific word, not the other way round.

推荐答案

我建议使用 String + toLowerCase 并使用 String#indexOf ,因为它适用于所有浏览器。

I suggest to use String+toLowerCase and check with String#indexOf, because it works in every browser.

if (gewaesser_name1.toLowerCase().indexOf("stream") === -1 && gewaesser_name2.toLowerCase().indexOf("stream") === -1) {
    var a = "..."
}

这篇关于如何正确检查字符串是否不包含特定单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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