Javascript

Some theorie

Shorthand Character Classes

Sign Description
\w [a-Z] and [0-9] and _
\s All kind of whitespaces
\n, \r, \t Carriage Return, Tab, Line Break
\d Decimals
\p{} All kind of signs with property X
\W ~\w
\S ~\s
\D ~\D
\P{} ~\p{}
. Everything except \n
^
Beginning of line
$ End of line

Modifier

/Regular Expression/g → Global matching, do not stop after first match
/Regular Expression/i → Case insensitive

Quantifier

Sign Description
? {0,1}
+ {1,}
* {0,}
{n} n signs
{n,} n or more signs
{,m} at most m signs
{n,m} between n and m signs

Grouping

Working with two groups:
'http://www.google.ch'.replace(/(http:\/\/(\S*))/g, '<a href=“$1”>$2<\/a>');

→ ”<a href=“http://www.google.ch”>www.google.ch</a>”

Non matching group start with ?:
'http://www.google.ch'.replace(/(?:http:\/\/(\S*))/g, '<a href=“$1”>$2<\/a>');
→ ”<a href=“www.google.ch”>$2</a>”

Examples

'%aa%bb%c%'.match(/\w{2,}/) → [“aa”]
'%aa%bb%c%'.match(/\w{2,}/g) → [“aa”, “bb”]
'Hallo+Fan'.match(/\++/); → [”+”]
'ab%07cd%1Bef%0Aghi%00jk'.replace(/%[01][a-f\d]?/gi,””); → 'abcdefghijk' (to remove ascii-characters in the range %00 to %20)
'http://www.google.ch'.replace(/(http:\/\/\S*)/g, '<a href=“$1”>$1<\/a>') → ”<a href=“http://www.google.ch”>http://www.google.ch</a>”

Links

 
wettstein/regularexpressions.txt · Last modified: 2009-04-03 09:24 by frank.wettstein
 
Recent changes RSS feed Creative Commons License Powered by GNU/Linux Powered by Gentoo Powered by Apache Powered by XCache Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki