Javascript正则表达式

Preview:

DESCRIPTION

 

Citation preview

Javascript Regular Expressions

@laserji 20110817

What is?

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

Regex flavors

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

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.

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

Matching Modes

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

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

Javascript Methods

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

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

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

Caution

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

+/)• Don’t be greedy

Tools

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

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

Resoures

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

Recommended