JavaScript 中 '/i' 标志的重要性是什么?
在了解 I 标志之前,让我们先了解一下正则表达式。创建搜索模式的字符串称为正则表达式。搜索模式可应用于文本替换和搜索操作。正则表达式可能是一个带有一个字母的简单模式,也可能是一个更复杂的模式。可以使用正则表达式执行各种文本搜索和替换操作。
文字符号和构造函数是创建 RegExp 对象的两种方法。
文字符号由两个斜杠之间的模式和第二个斜杠以外的可选标志组成。在 JavaScript 中,使用花括号{} 声明对象。花括号{} 是对象文字语法,这意味着以这种方式写出新对象称为构造对象文字。每当正则表达式保持不变时,使用文字符号就至关重要。例如,不建议在循环中使用文字符号。
可选标志字符串是构造函数的第二个参数,否则,该函数将接受字符串或 RegExp 对象作为其第一个参数。使用构造函数允许程序员在 JavaScript 中编写正则表达式。此方法以字符串的形式接受正则表达式作为函数参数。从 ECMAScript 6 开始,构造函数现在可以接受正则表达式中的文字。在编写正则表达式时,如果其模式在执行时会发生变化,最好使用构造函数。
您可以使用模式修饰符来控制如何处理模式匹配。模式修饰符紧接着正则表达式添加;例如,您可以使用 I 修饰符来搜索模式,无论大小写如何,只需键入 /pattern/i。
正则表达式的 I 标志是否使用取决于 ignoreCase 属性。特定正则表达式类型的只读属性 ignoreCase。
如果使用了 I 标志,ignoreCase 值将为 true;否则将为 false。为了在字符串中找到匹配项,I 标志指示必须忽略此大小写。
对于字符串中不区分大小写的匹配,JavaScript 使用 RegExp I 修饰符。
语法
以下是 Javascript 中"/I"标志的语法
/regexp/i
或者
new RegExp("regexp", "i")
示例 1
让我们看看 I 修饰符在此示例中如何工作,以演示不区分大小写的匹配。
<!DOCTYPE html> <html> <title>What is the importance of '/i' flag in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div id="result"></div> <script> let siteTitle = "Visit Tutorialspoint to know more about coding"; let regExp = /Tutorialspoint/i; let matchResult = siteTitle.match(regExp); document.getElementById("result").innerHTML = matchResult; </script> </body> </html>
示例 2
在此示例中,让我们了解一下 ingnoreCase 的使用。如果以某种方式使用了 I 标志,则 ignoreCase 值将为 true;否则,将为 false。为了在字符串中找到匹配项,I 标志指示应忽略大小写。
此属性不可直接编辑。
<!DOCTYPE html> <html> <title>What is the importance of '/i' flag in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div id="result"></div> <script> let regIgno = new RegExp('tutorialspoint', 'i'); document.getElementById("result").innerHTML =regIgno.ignoreCase; </script> </body> </html>
示例 3
前者通常用于正则表达式不能动态的情况,例如涉及用户定义的表达式时,后者用于正则表达式保持不变的情况。您可以通过将 I 标志传递给正则表达式来指示正则表达式执行不区分大小写的替换 −
<!DOCTYPE html> <html> <title>What is the importance of '/i' flag in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <script> const regExp = /JavaScript/i; let origString = 'Tutorialspoint is the best platform for JavaScript developers.'; let newString = origString.replace(regExp, 'React Js'); document.write(origString +"<br>"); document.write(newString); </script> </body> </html>
示例 4
在此示例中,让我们了解如何使用 RegExp 函数 test()
<!DOCTYPE html> <html> <title>What is the importance of '/i' flag in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div id="result"></div> <script> let siteTitle = "Visit Tutorialspoint"; let regExp = /Tutorialspoint/i; let testResult = regExp.test(siteTitle); document.getElementById("result").innerHTML = testResult; </script> </body> </html>