javascript正则表达式

12
Javascript Regular Expressions @laserji 20110817

Upload: ji-guang

Post on 12-Nov-2014

882 views

Category:

Technology


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Javascript正则表达式

Javascript Regular Expressions

@laserji 20110817

Page 2: Javascript正则表达式

What is?

• A regular expression (regex or regexp for short) is a special text string for describing a search pattern.

Page 3: Javascript正则表达式

Regex flavors

• .NET• Java• Javascript• Perl• POSIX• PCRE• Python• Ruby• …

Page 4: Javascript正则表达式

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.

Page 5: Javascript正则表达式

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

Page 6: Javascript正则表达式

Matching Modes

• /i makes the regex match case insensitive.• /m enables "multi-line mode". • /g enables "global" matching.

Page 7: Javascript正则表达式

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

Page 8: Javascript正则表达式

Javascript Methods

• String– String.split()– String.replace()– String.search()– String.match()

• RegExp– RegExp.test()– RegExp.exec()

Page 9: Javascript正则表达式

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,capture1)

{ return capture1+'456'}) ->’123456’• '120X120'.split('X')[0] ->120

Page 10: Javascript正则表达式

Caution

• String.match() with /g• String.split() with capture group• ‘abc’.match(/(\w+)/) vs. ‘abc’.match(/(\w)

+/)• Don’t be greedy

Page 11: Javascript正则表达式

Tools

• https://addons.mozilla.org/en-US/firefox/addon/regular-expressions-tester/

• http://gskinner.com/RegExr/• And:

Page 12: Javascript正则表达式

Resoures

• http://regexlib.com/• http://www.regexlab.com/zh/• more on Google