JavaScript RegExp i 修飾符
- 上一頁 g
- 下一頁 d
- 返回上一層 JavaScript RegExp 參考手冊
定義和用法
"i" 修飾符規定不區分大小寫的匹配。
實例
例子 1
對 "is" 進行不區分大小寫的搜索:
let text = "Visit CodeW3C.com"; let pattern = /codew3c/i; let result = text.match(pattern);
例子 2
在字符串中對 "codew3c" 進行不區分大小寫的搜索:
使用正則表達式函數 exec():
let text = "Visit codew3c"; let pattern = /codew3c/i; let result = pattern.exec(text);
例子 3
使用正則表達式函數 test()
:
let text = "Visit CodeW3C.com"; let pattern = /codew3c/i; let result = pattern.test(text);
例子 4
使用字符串函數 match()
:
let text = "Visit CodeW3C.com"; let pattern = /codew3c/i; let result = text.match(pattern);
語法
new RegExp("regexp", "i")
或者簡寫:
/regexp/i
正則表達式搜索方法
在 JavaScript 中,正則表達式文本搜索可以用不同的方法完成。
使用模式(pattern)作為正則表達式,這些是最常用的方法:
舉例 | 描述 |
---|---|
text.match(pattern) | 字符串方法 match() |
text.search(pattern) | 字符串方法 search() |
pattern.exec(text) | RexExp 方法 exec() |
pattern.test(text) | RexExp 方法 test() |
瀏覽器支持
/regexp/i
是 ECMAScript1 (ES1) 特性。
所有瀏覽器都完全支持 ES1 (JavaScript 1997):
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
支持 | 支持 | 支持 | 支持 | 支持 | 支持 |
- 上一頁 g
- 下一頁 d
- 返回上一層 JavaScript RegExp 參考手冊