Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Javascript正则表达式

Ji Guang
November 09, 2012
690

 Javascript正则表达式

Ji Guang

November 09, 2012
Tweet

Transcript

  1. What is? • A regular expression (regex or regexp for

    short) is a special text string for describing a search pattern.
  2. Regex flavors • .NET • Java • Javascript • Perl

    • POSIX • PCRE • Python • Ruby • …
  3. Javascript’s Flavor • No \A or \Z anchors to match

    the start or end of the string. Use a caret or dollar instead. • Lookbehind is not supported at all. Lookahead is fully supported. • No atomic grouping or possessive quantifiers • No Unicode support, except for matching single characters with • No named capturing groups. Use numbered capturing groups instead. • No mode modifiers to set matching options within the regular expression. • No conditionals. • No regular expression comments. Describe your regular expression with JavaScript // comments instead, outside the regular expression string.
  4. Basics • . – Matches any character, except for line

    breaks if dotall is false. • * – Matches 0 or more of the preceding character. • + – Matches 1 or more of the preceding character. • ? – Preceding character is optional. Matches 0 or 1 occurrence. • \d – Matches any single digit • \w – Matches any word character (alphanumeric & underscore). • [XYZ] – Matches any single character from the character class. • [XYZ]+ – Matches one or more of any of the characters in the set. • $ – Matches the end of the string. • ^ – Matches the beginning of a string. • [^a-z] – When inside of a character class, the ^ means NOT; in this case, match anything that is NOT a lowercase letter. • {3,} – Limiting Repetition
  5. Matching Modes • /i makes the regex match case insensitive.

    • /m enables "multi-line mode". • /g enables "global" matching.
  6. Advanced • Character Classes/Sets – [cat] or [^cat] [\w+] •

    Capture group & Backreference – (capture)\1 (?:notcapture) • Laziness & Greediness – <a href=“#” title=“x”> – <.*>, <[^>]*>, <.*?> • Lookahead & Lookbehind – (?=xxx) • Unicode – /\u00A5/.test('¥') ->true
  7. Javascript Methods • String – String.split() – String.replace() – String.search()

    – String.match() • RegExp – RegExp.test() – RegExp.exec()
  8. Examples • /\d{5}/.test(55555) -> true • ‘paipai’.replace(/i/g,’’) -> ‘papa’ •

    '#369'.replace(/(\d)/g,'$1$1') -> #336699 • '12345'.replace(/\d/g,function(match){ return match*2}) ->’246810’ • 'abc'.replace(/\w/,function(m){ return m.toUpperCase()} ) -> ‘Abc’ • '123abc'.replace(/(\d+)\w+/g,function(match,cap ture1){ return capture1+'456'}) ->’123456’ • '120X120'.split('X')[0] ->120
  9. Caution • String.match() with /g • String.split() with capture group

    • ‘abc’.match(/(\w+)/) vs. ‘abc’.match(/(\w)+/) • Don’t be greedy