可以使用
string.toUpperCase 执行 Javascript 字符串不区分大小写的比较。
var x = document.getElementById('MY_DIV').name;
x = x.toUpperCase(); //use this line
if (x == "STRING TO MATCH"){
}
参考tutorialsPoint使用 ignoreCase 属性的示例
<html>
<head>
<title>JavaScript RegExp ignoreCase Property</title>
</head>
<body>
<script type="text/javascript">
var re = new RegExp( "string" );
if ( re.ignoreCase ){
document.write("Test1-ignoreCase property is set");
}else{
document.write("Test1-ignoreCase property is not set");
}
re = new RegExp( "string", "i" );
if ( re.ignoreCase ){
document.write("<br/>Test2-ignoreCase property is set");
}else{
document.write("<br/>Test2-ignoreCase property is not set");
}
</script>
</body>
</html>
输出
Test1 - 未设置 ignoreCase 属性
Test2 - 设置了 ignoreCase 属性
为 DK 功能更新代码
function myfunc()
{
var x = document.getElementById('MY_DIV').name;
//x = x.toUpperCase(); // check the result with / without un-commenting this line
var re = new RegExp( x );
if ( re.ignoreCase ){
document.write("X - Test1-ignoreCase property is set");
}else{
document.write("X - Test1-ignoreCase property is not set");
}
re = new RegExp( x, "i" );
if ( re.ignoreCase ){
document.write("<br/> X - Test2-ignoreCase property is set");
}else{
document.write("<br/>X - Test2-ignoreCase property is not set");
}
x = x.toUpperCase(); // ignoring case
document.getElementById(x).value=2;
}