.match()
搜尋資料.match()
的括號內填入要取用的規則,.match()
點的前方加入要取用的所有陣列、字串。
.match(正規表達式)
str.match()
括號內通常會放入一個正規表達式,
當該字串與正規表達式的結果符合時,會放入符合的結果到一個新陣列中。
//要比對的字串
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
//要找出範圍 A-E
//g:比對該字串所有位置
//i:不去區分大小寫
//宣告一個變數賦予正規表達式
var regexp = /[A-E]/gi;
var matches_array = str.match(regexp);
console.log(matches_array);
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
.match()
搭配 filter
優化搜尋功能範例:搜尋下方內容
JS
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up