Murphy Tsai
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# [Javascript] 初探Regex 正規表達式 正規表達式英文全名Regular Expression, 想必一定曾是每個工程師的惡夢,在新手眼裡彷彿精靈語一樣神秘又難親近, Regex是一把複雜卻威力強大的武器。如果可以活用,在很多場合可以幫助你少寫很多的if判斷式,今天這篇文章就要來幫助理解正規表達式。 ## 為什麼需要 Regular Expression? Regex可以實用的情境大致有幾種: - 尋找匹配的字串 - 取代匹配的字串 - 驗證使用者輸入資料欄位 - 擷取某段想要的資訊 舉例Js為例子來說,如果我想要看看資料裡面有沒有某個字元"M",一個簡單的方式是這樣寫: ``` JS let stringMatch = "This is Mujing.".some(s=>{ return s=="M" }) // Check if there is M in my data. ``` 不過如果今天,當條件變得更多更複雜的時候這時候正規表達式就派上用場了。 (字串裡面必須包含一個數字或一個大寫字母): ```js let re = "" let result = "uSer0910".match(/([0-9]|[A-Z])/g) //result.length = 1 ``` ## Regular Expression 規則說明 正規表達式使用時是將規則寫在兩個正的斜線裡面,並且撰寫的時候有幾種類型的匹配方式: 1. 直接匹配: 例如直接輸入字元"abc".match(/a/) 就可以匹配到a字元。 2. 使用跳脫字元匹配:有些英文字在Regex裡面代表的特殊意義,但因為是用英文字元代表的,所以在使用時要用“\”跳脫字元來告知這不是一個直接匹配的字元。 3. 特殊字元匹配:跟第二種相反,有一些特殊符號一但出現,就代表某種意義,如“[]“就代表任意匹配,如果要單純匹配 "\[" 字元,則必須用反斜線告知。 ## JS 的Regular Expression寫法 JS裡面寫正規表達式的時候你可以直接用兩個斜線代表,或是new一個RegExp物件並將要寫的規則寫入建構子,不要用這種做法要注意,RegExp預設會直接幫你跳脫特殊字元,如果你要在裡面寫跳脫字元規則的話,記得寫兩個反斜線來告訴他不要跳脫: 例如: ```js new RegExp('\d') // return /d/ (變成匹配d這個字元了) ``` ```js new RegExp('\\d') // return /\d/ (是我要的數字匹配規則) ``` RegExp物件底下有一個test方法,用法如下: ``` js /a/.test('An Apple a day.') // return true ``` 就可以很快的測試自己寫的方法對不對了,這個方法用在表單檢查也非常好用。 正規表達式還可以用在很多地方,如match、split、replace等操作字串的方法, 學會之後想怎麼用就怎麼用。 以下整理正規表達式常用的特殊規則,或可參考[JS官方文件](https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Guide/Regular_Expressions) ## 跳脫字元規則 這邊有個好記的規則,小寫的 | 符號 | 說明 | | -------- | -------- | | \d | 匹配任意數字| | \D | 匹配任意非數字字元| | \w | 匹配所有文字字元 \(a-z、A-Z、0-9、_ \) | | \W | 匹配所有“非”文字字元 \(標點符號、特殊字元) | \s | 匹配空白字元| | \S | 匹配“非”空白字元| | \b | 匹配字元邊界 /(空格或開頭/)| | \B | 匹配字元邊界| ## 特殊符號 | 符號 | 匹配說明 | | -------- | -------- | | \ | 反斜線,跳脫特殊字元,例如想尋找"/"的時候| | . | 任意字元| | $ | 字元結尾| | ^ | 字元開頭| | [] |中括號,比對中括號裡面的任一字元,可以用範圍匹配:[A-Z]、[a-z]、[0-9]| | [^] |^代表「反」,比對中括號裡面"以外"的任一字元| | \| | 同程式常見的OR邏輯| | () | 群組| ## 指定匹配次數 因為一個字元只會匹配一次,如果要多次匹配,就可以用指定字數的規則。 需注意:**這些次數針對的是的是該該符號擺放位置的前一個匹配規則。** | 符號 | 匹配次數 | | -------- | -------- | | * | 0次或更多| | + | 1次或更多| | ? | 0次或1次| | {m} | m次| | {n,} |最少n次| | {m,n} |從m次到n次| | {m,n}?| 從m次到n次,選到匹配最少次的| ## 基礎範例: 其實懂了基本邏輯之後,剩下的就是組合而已,匹配的規則要用在查就可以了,因為比較常用的寫法都差不多。 ### 1.驗證手機號碼: 手機號碼有10個數字,要怎麼寫呢?你可能第一個會想到[0-9],方向對了, 但[0-9]只會匹配一次,如果要匹配10次可以這麼寫: ``` js /[0-9]{10}/ ``` ### 2.商品型號: 一般電商在後台上架商品的時候,一個常見的需求是輸入商品型號,為了減少使用者錯誤率也為了提高效率,我們可以用正規表達式解決,假設今天商品型號的規則是: 三個英文字母 + “-” + 五個數字 (例如ABC-90006 ) 那麼其實搭配上面講的匹配次數就可以: ``` js /[A-Z]{3}-[0-9]{5}/ ``` ### 3.驗證密碼(至少包含一個大寫字母以及一個數字): 這個情況想必跟[A-Z]以及[0-9]脫不了關係,但會遇到順序性的問題,直接給[A-Z][0-9]代表的是先一個大寫字母在加上一個數字,不能夠反過來,這時就會需要最少匹配一次的"+"跟最寬容的"*"。 拆解各種情況: [A-Z][0-9] :匹配一個A-Z的字母跟一個0-9的數字,而且不能顛倒 [A-Z]+ :代表匹配大寫字母最少一次 [A-Z]* :代表匹配A-Z0次或更多(這樣就代表可有可無,不是我們想要的) [A-Z]+[0-9]+ :匹配[A-Z]最少一次之後在匹配0-9最少一次(如:AABB909) 看完上面四個範例你應該就會理解剛剛講的順序性是什麼,根據這個條件,可能會有的情況有幾種: 1. [任意字串] + [至少一個A-Z字元]+[至少一個數字]+[任意字串] 2. [A-Z字元在開頭]+[任意字串]+[至少一個數字] 簡單來說就是要考慮我要匹配的字元是在開頭以及結尾,我們可以用 “.*”來達成 [ 任意字符 ]的[ 匹配任意次(包含0次)],以及 "+" 達成最少一次的匹配: .*[A-Z]+ :表示至少有一個A-Z字元,前面可以有其他「任意次」個「任意字元」。 組合!! ```js .*[A-Z]+.*[0-9]+.* ``` 這樣就可以達到不管前面有什麼,最後有什麼,只要有一個A-Z跟一個數字,就符合規則啦! 但最後還要考慮順序的問題,可以用 OR 邏輯將數字跟字母對調,就完美符合我們的需求啦! ``` js .*[A-Z]+.*[0-9]+.* | .*[0-9]+.*[A-Z]+.* ``` ## 幾個練習Regex的網站: 比較推薦第一個,他是互動式的,一步一步帶你了解正規表達式的使用方式,說明的很詳細。 剩下兩個則是自己在測試的時候很好用,寫完表達式右邊還有規則的說明,非常方便。 1. [RegexOne](https://regexone.com/problem/matching_decimal_numbers) 2. [Regex101](https://regex101.com/) 3. [Regexr](https://regexr.com/)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

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

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully