繁体   English   中英

Javascript如何在字符串中具有随机大写字母时使用.includes()

[英]Javascript how to use .includes() when you can hav random capital letters in a string

我需要制作一个获取用户输入字符串的程序。 如何检查带有.includes()的特殊单词是否可以包含大写字母?

最简单的方法是摆脱大写字母

inputString.toLowerCase().includes(str)

否则,您将需要其他解决方案,例如正则表达式。

根据MDN String.includes()规范

include()方法区分大小写。 例如,以下表达式返回false:

'蓝鲸'.includes('blue'); //返回false

因此,如果要使用String.includes()并要检查变量数据,则应在调用.includes()之前使用将字符串转换为小写的解决方案。

如果特定单词的含义是它始终与您要检查的单词相同(或字符串,它可以有空格),则可以选择正则表达式:

// using the i flag here you check for the word multycaseworld case insensitively
var reggex = /\bmultycaseworld\b/i   // adding \b to match word boundary (avoiding cat to match both cat and caterpillar)
var string = 'some string with MulTyCaseWorld'

var result = string.match(reggex)
//result will hold an array with the match so if result.length > 0 then you have a match

https://jsfiddle.net/0gc5rpdv/3/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM