Javascript正则表达式

916 views

Published on

Published in: Technology
  • Be the first to comment

Javascript正则表达式

  1. 1. Javascript Regular Expressions<br />@laserji 20110817<br />
  2. 2. What is?<br />A regular expression (regex or regexp for short) is a special text string for describing a search pattern.<br />
  3. 3. Regex flavors<br />.NET<br />Java<br />Javascript<br />Perl<br />POSIX<br />PCRE<br />Python<br />Ruby<br />…<br />
  4. 4. Javascript’s Flavor<br />No A or Z anchors to match the start or end of the string. Use a caret or dollar instead.<br />Lookbehind is not supported at all. Lookahead is fully supported.<br />No atomic grouping or possessive quantifiers<br />No Unicode support, except for matching single characters with<br />No named capturing groups. Use numbered capturing groups instead.<br />No mode modifiers to set matching options within the regular expression.<br />No conditionals.<br />No regular expression comments. Describe your regular expression with JavaScript // comments instead, outside the regular expression string.<br />
  5. 5. Basics<br />. – Matches any character, except for line breaks if dotall is false.<br />* – Matches 0 or more of the preceding character.<br />+ – Matches 1 or more of the preceding character.<br />? – Preceding character is optional. Matches 0 or 1 occurrence.<br />d – Matches any single digit<br />w – Matches any word character (alphanumeric & underscore).<br />[XYZ] – Matches any single character from the character class.<br />[XYZ]+ – Matches one or more of any of the characters in the set.<br />$ – Matches the end of the string.<br />^ – Matches the beginning of a string.<br />[^a-z] – When inside of a character class, the ^ means NOT; in this case, match anything that is NOT a lowercase letter.<br />{3,} – Limiting Repetition<br />
  6. 6. Matching Modes<br />/i makes the regex match case insensitive.<br />/m enables "multi-line mode". <br />/g enables "global" matching. <br />
  7. 7. Advanced<br />Character Classes/Sets<br />[cat] or [^cat] [w+]<br />Capture group & Backreference<br />(capture)1 (?:notcapture)<br />Laziness & Greediness<br /><a href=“#” title=“x”><br /><.*>, <[^>]*>, <.*?><br />Lookahead & Lookbehind<br />(?=xxx)<br />Unicode<br />/u00A5/.test('¥') ->true<br />
  8. 8. Javascript Methods<br />String<br />String.split()<br />String.replace()<br />String.search()<br />String.match()<br />RegExp<br />RegExp.test()<br />RegExp.exec()<br />
  9. 9. Examples<br />/d{5}/.test(55555) -> true<br />‘paipai’.replace(/i/g,’’) -> ‘papa’<br />'#369'.replace(/(d)/g,'$1$1') -> #336699<br />'12345'.replace(/d/g,function(match){ return match*2}) ->’246810’<br />'abc'.replace(/w/,function(m){ return m.toUpperCase()} ) -> ‘Abc’<br />'123abc'.replace(/(d+)w+/g,function(match,capture1){ return capture1+'456'}) ->’123456’<br />'120X120'.split('X')[0] ->120<br />
  10. 10. Caution<br />String.match() with /g<br />String.split() with capture group<br />‘abc’.match(/(w+)/) vs. ‘abc’.match(/(w)+/)<br />Don’t be greedy<br />
  11. 11. Tools<br />https://addons.mozilla.org/en-US/firefox/addon/regular-expressions-tester/<br />http://gskinner.com/RegExr/<br />And:<br />
  12. 12. Resoures<br />http://regexlib.com/<br />http://www.regexlab.com/zh/<br />more on Google<br />

×