Contents
- 組成元件
- 不使用特殊字元
- 使用特殊字元
- 生活實例
組成元件
- 目標字串
- 要被搜尋的字串
- 搜尋的關鍵字
- 從目標字串中找出是否存在所要的關鍵字
- JavaScript 使用方式
- new RegExp(
pattern
[ ,flags
] )pattern
代表搜尋的關鍵字flags
的部份可有可無- 種類
g
全域搜尋 【 參考連結 】i
不區分大小寫m
多行搜尋y
從指定的位置開始搜尋
- 例如
- 目標字串
- The latest airplane designs evolved from slabcraft
- 搜尋的關鍵字
- airplane
- 說明
- 從
The latest airplane designs evolved from slabcraft
的目標字串中找是否存在airplane
的關鍵字
- 從
- 目標字串
不使用特殊字元
- 亦即是忽略英文大小寫的完全字串比對
【 JavaScript 程式 - 1 】
const targetString = 'hello world'
const pattern = /world/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
hello world 的目標字串中是否存在與 world 完全相同的搜尋關鍵字,
但忽略英文大小寫 ( 因 flag 為 i )
【 JavaScript 程式 - 2 】
const targetString = 'hello worlD'
const pattern = /world/
const regExp = new RegExp(pattern)
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
hello worlD 的目標字串中是否存在與 world 完全相同的搜尋關鍵字,
但區分英文大小寫
使用特殊字元
亦即是搜尋關鍵字中使用特殊字元,種類如下所示:
*
代表前一個字元出現 >= 0 次+
代表前一個字元出現 >= 1 次?
代表前一個字元出現 0 次 或 1 次^
代表開頭處$
代表結尾處.
代表 1 個字元x|y
代表符合x
或y
{n}
代表符合確切發生的次數,其中 n 為正整數{n,m}
代表符合出現的次數為n
到m
之間,其中n
與m
皆為正整數[xyz]
代表符合括號中的字元[^xyz]
代表不符合括號中的字元[0-9]
代表 0 ~ 9 間的數字\d
代表 0 ~ 9 間的數字\D
代表非 0 ~ 9 間的數字,等同於 [^0-9]\w
代表任何數字、字母、底線,等同於 [A-Za-z0-9_]\W
代表任何非數字、字母、底線,等同於 [^A-Za-z0-9_]
【 種類 ① 】
*
代表前一個字元出現 >= 0 次- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worL'
const pattern = /world*/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worL
② 搜尋關鍵字為 world*
③ hello worL 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 world* 進行拆解成
◆ 前半部為 worl
◆ 後半部為 d*
★ 前半部的 worl 字串一定要出現且完全相同,
但忽略英文大小寫 ( 因 flag 為 i )
★ 緊接著要出現後半部的 d 字元可出現 >= 0 次
( 因 * 代表出現 >= 0 次 )
【 JavaScript 程式 - 2 】
const targetString = 'hello worLdd'
const pattern = /world*/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLdd
② 搜尋關鍵字為 world*
③ hello worLdd 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 world* 進行拆解成
◆ 前半部為 worl
◆ 後半部為 d*
★ 前半部的 worl 字串一定要出現且完全相同,
但忽略英文大小寫 ( 因 flag 為 i )
★ 緊接著要出現後半部的 d 字元可出現 >= 0 次
( 因 * 代表出現 >= 0 次 )
- 【 種類 ② 】
+
代表前一個字元出現 >= 1 次- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worL'
const pattern = /world+/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worL
② 搜尋關鍵字為 world+
③ hello worL 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 world+ 進行拆解成
◆ 前半部為 worl
◆ 後半部為 d+
★ 前半部的 worl 字串一定要出現且完全相同,
但忽略英文大小寫 ( 因 flag 為 i )
★ 緊接著要出現後半部的 d 字元可出現 >= 1 次
( 因 + 代表出現 >= 1 次 )
【 JavaScript 程式 - 2 】
const targetString = 'hello worLdd'
const pattern = /world+/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLdd
② 搜尋關鍵字為 world+
③ hello worLdd 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 world+ 進行拆解成
◆ 前半部為 worl
◆ 後半部為 d+
★ 前半部的 worl 字串一定要出現且完全相同,
但忽略英文大小寫 ( 因 flag 為 i )
★ 緊接著要出現後半部的 d 字元可出現 >= 1 次
( 因 + 代表出現 >= 1 次 )
- 【 種類 ③ 】
?
代表前一個字元出現 0 次 或 1 次- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worLabc'
const pattern = /world?a/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLabc
② 搜尋關鍵字為 world?a
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 world?a 進行拆解成
◆ 前半部為 worl
◆ 中半部為 d?
◆ 後半部為 a
★ 前半部的 worl 字串一定要出現且完全相同,
但忽略英文大小寫 ( 因 flag 為 i )
★ 緊接著要出現中半部為 d 字元出現 0 次 或 1 次
( 因 ? 代表出現 0 次 或 1 次 )
★ 緊接著要出現後半部的 a 一定要出現且完全相同,
但忽略英文大小寫 ( 因 flag 為 i )
【 JavaScript 程式 - 2 】
const targetString = 'hello worLddabc'
const pattern = /world?a/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 world?a
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 world?a 進行拆解成
◆ 前半部為 worl
◆ 中半部為 d?
◆ 後半部為 a
★ 前半部的 worl 字串一定要出現且完全相同,
但忽略英文大小寫 ( 因 flag 為 i )
* 緊接著要出現中半部為 d 字元出現 0 次 或 1 次
( 因 ? 代表出現 0 次 或 1 次 )
★ 緊接著要出現後半部的 a 一定要出現且完全相同,
但忽略英文大小寫 ( 因 flag 為 i )
- 【 種類 ④ 】
^
代表開頭處- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worLddabc'
const pattern = /^hello/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 ^hello
③ hello worLabc 的目標字串中是否符合下列條件
★ 搜尋關鍵字 ^hello 代表句首要為 hello,
但忽略英文大小寫 ( 因 flag 為 i )
【 JavaScript 程式 - 2 】
const targetString = 'hello worLabc'
const pattern = /^world/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 ^world
③ hello worLabc 的目標字串中是否符合下列條件
★ 搜尋關鍵字 ^world 代表句首要為 world,
但忽略英文大小寫 ( 因 flag 為 i )
- 【 種類 ⑤ 】
$
代表結尾處- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worLabc'
const pattern = /hello$/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 hello$
③ hello worLabc 的目標字串中是否符合下列條件
★ 搜尋關鍵字 hello$ 代表句尾要為 hello,
但忽略英文大小寫 ( 因 flag 為 i )
【 JavaScript 程式 - 2 】
const targetString = 'hello worLddabc'
const pattern = /abc$/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 abc$
③ hello worLabc 的目標字串中是否符合下列條件
★ 搜尋關鍵字 abc$ 代表句尾要為 abc,
但忽略英文大小寫 ( 因 flag 為 i )
- 【 種類 ⑥ 】
.
代表 1 個字元- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worLdd1abc'
const pattern = /.abc/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLdd1abc
② 搜尋關鍵字為 .abc
③ hello worLdd1abc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 .abc 進行拆解成
◆ 前半部為 .
◆ 後半部為 abc
★ 目標字串中包含前半部的 1 個字元,緊接著包含後半部的 abc 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
【 JavaScript 程式 - 2 】
const targetString = 'hello worLdd1abc'
const pattern = /.abcd/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLdd1abc
② 搜尋關鍵字為 .abcd
③ hello worLdd1abc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 .abcd 進行拆解成
◆ 前半部為 .
◆ 後半部為 abcd
★ 目標字串中包含前半部的 1 個字元,緊接著包含後半部的 abcd 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即未符合條件
- 【 種類 ⑦ 】
x|y
代表符合x
或y
- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worLabc'
const pattern = /ell|qwer/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 ell|qwer
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 ell|qwer 進行拆解成
◆ 前半部為 ell
◆ 後半部為 qwer
★ 目標字串中包含前半部的 ell 字串,但忽略英文大小寫 ( 因 flag 為 i )
或包含後半部的 qwer 字串,但忽略英文大小寫 ( 因 flag 為 i ) ,
即符合條件
【 JavaScript 程式 - 2 】
const targetString = 'hello worLddabc'
const pattern = /qq|dd/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 qq|dd
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 qq|dd 進行拆解成
◆ 前半部為 qq
◆ 後半部為 dd
★ 目標字串中包含前半部的 qq 字串,但忽略英文大小寫 ( 因 flag 為 i )
或包含後半部的 dd 字串,但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
【 JavaScript 程式 - 3 】
const targetString = 'hello worLddabc'
const pattern = /qq|ddd/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 qq|ddd
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 qq|dd 進行拆解成
◆ 前半部為 qq
◆ 後半部為 ddd
★ 目標字串中包含前半部的 qq 字串,但忽略英文大小寫 ( 因 flag 為 i )
或包含後半部的 ddd 字串,但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
- 【 種類 ⑧ 】
{n}
代表符合確切發生的次數,其中n
為正整數- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worLddabc'
const pattern = /el{2}/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 el{2}
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 el{2} 進行拆解成
◆ 前半部為 e
◆ 後半部為 l{2}
★ 目標字串中包含前半部的 e 字串,但忽略英文大小寫 ( 因 flag 為 i ) ,
緊接著包含後半部的 l 字串至少 2 個 ( 因 l{2} ),
但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
【 JavaScript 程式 - 2 】
const targetString = 'hello worLddabc'
const pattern = /el{3}/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 el{3}
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 el{3} 進行拆解成
◆ 前半部為 e
◆ 後半部為 l{3}
★ 目標字串中包含前半部的 e 字串,但忽略英文大小寫 ( 因 flag 為 i ) ,
緊接著包含後半部的 l 字串至少 3 個 ( 因 l{3} ),
但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
- 【 種類 ⑨ 】
{n,m}
代表符合出現的次數為n
到m
之間,其中n
與m
皆為正整數- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worLddabc'
const pattern = /el{2,6}/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 el{2,6}
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 el{2,6} 進行拆解成
◆ 前半部為 e
◆ 後半部為 l{2,6}
★ 目標字串中包含前半部的 e 字串,但忽略英文大小寫 ( 因 flag 為 i ) ,
緊接著包含後半部的 l 字串出現 2 ~ 6 次 ( 因 l{2,6} ),
但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
【 JavaScript 程式 - 2 】
const targetString = 'hello worLddabc'
const pattern = /el{3,6}/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 el{3,6}
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 el{3,6} 進行拆解成
◆ 前半部為 e
◆ 後半部為 l{3,6}
★ 目標字串中包含前半部的 e 字串,但忽略英文大小寫 ( 因 flag 為 i ) ,
緊接著包含後半部的 l 字串出現 3 ~ 6 次 ( 因 l{3,6} ),
但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
- 【 種類 ⑩ 】
[xyz]
代表符合括號中的字元- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worLddabc'
const pattern = /[qo]rLdd/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 [qo]rLdd
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 [qo]rLdd 進行拆解成
◆ 前半部為 [qo]
◆ 後半部為 rLdd
★ 目標字串中包含前半部的 q 或 o 字元,緊接著包含後半部的 rLdd 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
【 JavaScript 程式 - 2 】
const targetString = 'hello worLddabc'
const pattern = /[ab]rLdd/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 [ab]rLdd
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 [ab]rLdd 進行拆解成
◆ 前半部為 [ab]
◆ 後半部為 rLdd
★ 目標字串中包含前半部的 a 或 b 字元,緊接著包含後半部的 rLdd 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即未符合條件
- 【 種類 ⑪ 】
[^xyz]
代表不符合括號中的字元- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worLddabc'
const pattern = /[^a]lo/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 [^a]lo
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 [^a]lo 進行拆解成
◆ 前半部為 [^a]
◆ 後半部為 lo
★ 目標字串中包含前半部的非 a 字元,緊接著包含後半部的 lo 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
【 JavaScript 程式 - 2 】
const targetString = 'hello worLddabc'
const pattern = /[^l]lo/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLddabc
② 搜尋關鍵字為 [^l]lo
③ hello worLabc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 [^l]lo 進行拆解成
◆ 前半部為 [^l]
◆ 後半部為 lo
★ 目標字串中包含前半部的非 l 字元,緊接著包含後半部的 lo 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即未符合條件
- 【 種類 ⑫ 】
[0-9]
代表 0 ~ 9 間的數字- 程式說明如下所示:
【 JavaScript 程式 - 1 】
const targetString = 'hello worLdd1abc'
const pattern = /[1-9]abc/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLdd1abc
② 搜尋關鍵字為 [1-9]abc
③ hello worLdd1abc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 [1-9]abc 進行拆解成
◆ 前半部為 [1-9]
◆ 後半部為 abc
★ 目標字串中包含前半部為 1 到 9 間的數字,緊接著包含後半部的 abc 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
【 JavaScript 程式 - 2 】
const targetString = 'hello worLdd1abc'
const pattern = /[6-9]abc/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLdd1abc
② 搜尋關鍵字為 [6-9]abc
③ hello worLdd1abc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 [6-9]abc 進行拆解成
◆ 前半部為 [6-9]
◆ 後半部為 abc
★ 目標字串中包含前半部為 6 到 9 間的數字,緊接著包含後半部的 abc 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即未符合條件
- 【 種類 ⑬ 】
\d
代表 0 ~ 9 間的數字- 程式說明如下所示:
【 JavaScript 程式 】
const targetString = 'hello worL1abc'
const pattern = /\dabc/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLdd1abc
② 搜尋關鍵字為 \dabc
③ hello worLdd1abc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 \dabc 進行拆解成
◆ 前半部為 \d
◆ 後半部為 abc
★ 目標字串中包含前半部為 0 到 9 間的數字,緊接著包含後半部的 abc 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
- 【 種類 ⑭ 】
\D
代表非 0 ~ 9 間的數字,等同於 [^0-9]- 程式說明如下所示:
【 JavaScript 程式 】
const targetString = 'hello worL1abc'
const pattern = /\Dabc/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLdd1abc
② 搜尋關鍵字為 \Dabc
③ hello worLdd1abc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 \Dabc 進行拆解成
◆ 前半部為 \D
◆ 後半部為 abc
★ 目標字串中包含前半部為非 0 到 9 間的數字,緊接著包含後半部的 abc 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即未符合條件
- 【 種類 ⑮ 】
\w
代表任何數字、字母、底線,等同於 [A-Za-z0-9_]- 程式說明如下所示:
【 JavaScript 程式 】
const targetString = 'hello worL1abc'
const pattern = /\wabc/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: true
【 說明 】
① 目標字串為 hello worLdd1abc
② 搜尋關鍵字為 \wabc
③ hello worLdd1abc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 \wabc 進行拆解成
◆ 前半部為 \w
◆ 後半部為 abc
★ 目標字串中包含前半部為任何數字、字母、底線,緊接著包含後半部的 abc 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即符合條件
- 【 種類 ⑯ 】
\W
代表任何非數字、字母、底線,等同於 [^A-Za-z0-9_]- 程式說明如下所示:
【 JavaScript 程式 】
const targetString = 'hello worL1abc'
const pattern = /\Wabc/
const regExp = new RegExp(pattern, 'i')
console.log(`存在關鍵字: ${regExp.test(targetString)}`)
【 Console 】
存在關鍵字: false
【 說明 】
① 目標字串為 hello worLdd1abc
② 搜尋關鍵字為 \Wabc
③ hello worLdd1abc 的目標字串中是否符合下列條件
★ 將搜尋關鍵字 \Wabc 進行拆解成
◆ 前半部為 \W
◆ 後半部為 abc
★ 目標字串中包含前半部為非任何數字、字母、底線,
緊接著包含後半部的 abc 字串,
但忽略英文大小寫 ( 因 flag 為 i ) ,即未符合條件
生活實例
身分證字號
A123456789/^[A-Z]\d{9}$/
手機號碼
0912345678/^09\d{8}$/
Gmail 信箱
[email protected]/^.*@gmail\.com$/
西元生日格式
1999-10-24/^\d{4}-\d{2}-\d{2}$/