1 / 12

Regular Expression in JavaScript

Regular Expression in JavaScript. 鄧姚文 http://www.ywdeng.idv.tw. Why Regular Expression. 許多複雜的字串搜尋與比對工作,無法用簡單的方式達成 Regular Expression ( RE, REGEX )已經成為業界標準 不止應用於程式語言中 JavaScript, JAVA, Perl, PHP, C#, VB.NET,… 也應用於許多主流作業系統的指令之中 Linux/UNIX, Windows PowerScript. Regular Expression Instances.

albina
Télécharger la présentation

Regular Expression in JavaScript

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Regular Expression in JavaScript 鄧姚文 http://www.ywdeng.idv.tw

  2. Why Regular Expression • 許多複雜的字串搜尋與比對工作,無法用簡單的方式達成 • Regular Expression(RE, REGEX)已經成為業界標準 • 不止應用於程式語言中 • JavaScript, JAVA, Perl, PHP, C#, VB.NET,… • 也應用於許多主流作業系統的指令之中 • Linux/UNIX, Windows PowerScript

  3. Regular ExpressionInstances • /Pattern/ • 正確: /^sender/ • 錯誤: "/^sender/" • var regex = new RegExp(pattern) • 引數 pattern 可以是一個字串也可以是另一個 RegExp 個體

  4. Regular Expression 常用符號 • ^ 該 pattern 出現在字串開頭 • $ 該 pattern 出現在字串結尾 • * 零個或多個前一符號的內容 • + 一個或多個前一符號的內容 • ? 零個或一個前一符號的內容 • . 一個字,任何字都 match • { } 前一符號在字串中的重覆次數

  5. Regular Expression 常用符號 • /^From/字串開頭為 "From" • /A{2}/ 'A' 重覆兩次 (即 'AA') • /A{2,}/字串含有 2 到無數多個 'A‘ • /A{2,5}/2 到 5 個 'A'

  6. Regular Expression 常用符號 • [ ] 字串含有括號中任一字元的內容。以 - 表示一組連續字元(範圍) • ( ) 表示一個 sub pattern • 符合 sub pattern 的字串內容會被存放在匹配陣列中,並依序指派數字代表此 sub pattern • 此數字在 pattern 的其他地方引用內容

  7. Regular Expression 常用符號 • /[a-z]/'a' ~ 'z' 之中任何一個小寫英文字元 • /[0-9]/'0' ~ '9' 之中任何一個阿拉伯數字 • /[abc]/表示 'a' 或 'b' 或 'c' • /The h([0-9]) means Title (\1)/ 第 1 個 sub pattern 是 0 到 9 的任一字元,而 \1 表示匹配的內容。'The h1 means Title 1', 'The h2 means Title 2' …'The h9 means Title 9' 皆符合規則

  8. RegExp 常用函式 • RegExp.exec(string) • 將 RegExp 套用到 string,傳回符合的資訊 • var match = /s(amp)le/i.exec("Sample text") • 則 match 的值為 ["Sample","amp"]

  9. RegExp 常用函式(續) • RegExp.test(string) • 測試 string 是否符合 Regexp • 符合則傳回 true • 不符合傳回 false • var match = /sample/.test("Sample text") • 則 match 的值為 false

  10. RegExp 常用函式(續) • String.search(pattern) • 將 RegExp 套用到 string,傳回符合處的索引值 • 如果不符合,傳回 -1 • 第一個字的索引值為 0 • var ndx = "Watch out for the rock!".search(/for/) • ndx 的值為 10

  11. RegExp 常用函式(續) • String1.replace(pattern, string2) • 將 String1 中符合的子字串替換成 string2 • 傳回替換後的結果 • 原本的 String1 內容並不會改變 • var str = "Liorean said: My name is Liorean!".replace(/Liorean/g,'Big Fat Dork') • Str 的內容為 "Big Fat Dork said: My name is Big Fat Dork!" • 參數 g 代表 global(全部替換)

  12. RegExp 常用函式(續) • String.split(pattern) • 以 pattern 為分隔符號,將 String 分割成許多個字串,傳回字串陣列 • var str = "I am confused".split(/\s/g) • 則 str 的值為 ["I","am","confused"]

More Related