javascript - javascript indexOf 不区分大小写

标签 javascript indexof case-insensitive

我有这个代码:

var textarea = document.getElementById('myTextarea');
var tag = '<!DOCTYPE html>';
var textValue = textarea.value;

if (textValue.indexOf(tag) != -1) {
  document.getElementById('status').innerHTML = "match found";
} else {
  document.getElementById('status').innerHTML = "no match found";
}

我正在尝试获取 indexOf匹配标签,无论 var标签是大写还是小写。我怎样才能做到这一点?

最佳答案

像这样使用toUpperCase()

var textarea = document.getElementById('myTextarea');
var tag = '<!DOCTYPE html>';  
var textValue=textarea.value;

if (textValue.toUpperCase().indexOf(tag.toUpperCase())!=-1)
{
   document.getElementById('status').innerHTML = "match found";
} else {
       document.getElementById('status').innerHTML = "no match found";
} 

这样,即使其中一个是小写,条件也会将两者比较为大写,因此不区分大小写

关于javascript - javascript indexOf 不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43790802/

相关文章:

javascript - 我应该从哪里调用 module.exports 来获取非空值?

java - 获取数组中对象的索引

javascript - IndexedDB 引用错误 : db is not defined

javascript - 在 Highcharts 中动态附加 onload 或 redraw 事件函数

javascript - 从证书 x509 中提取公钥

javascript indexOf 无法按预期使用数组

javascript - 如何返回包含值的索引列表

arrays - 是否可以在 PowerShell 中使 IndexOf 不区分大小写?

python - py3k : case-insensitive list sorting - With or WIthout lambda?

c# - MongoDB 和 C# : Case insensitive search