java脚本索引不是案例敏感

时间:2017-05-04 18:57:47

标签: 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标记是大写还是小写。我怎么能这样做呢?

1 个答案:

答案 0 :(得分:2)

像这样使用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";
} 

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