# 用 `.match()` 搜尋資料 `.match()` 的括號內填入要取用的規則,`.match()` 點的前方加入要取用的所有陣列、字串。 ## `.match(正規表達式)` `str.match()`括號內通常會放入一個正規表達式, 當該字串與正規表達式的結果符合時,會放入符合的結果到一個新陣列中。 ```javascript= //要比對的字串 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` 優化搜尋功能 範例:搜尋下方內容 <iframe height="300" style="width: 100%;" scrolling="no" title=".match() 搭配 filter 搜尋" src="https://codepen.io/unayo/embed/abwJxer?default-tab=js%2Cresult&theme-id=dark" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> See the Pen <a href="https://codepen.io/unayo/pen/abwJxer"> .match() 搭配 filter 搜尋</a> by unayo (<a href="https://codepen.io/unayo">@unayo</a>) on <a href="https://codepen.io">CodePen</a>. </iframe> ###### tags: `JS` {%hackmd @unayojanni/H1Qq0uKkK %}
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up