Thursday, December 8, 2011

Regular Expression Examples


^ The ^ (circumflex or caret) outside square brackets means look only at the beginning of the target string, for example, ^Win will not find Windows in STRING1 but ^Moz will find Mozilla.
$ The $ (dollar) means look only at the end of the target string, for example, fox$ will find a match in 'silver fox' since it appears at the end of the string but not in 'the fox jumped over the moon'. . The . (period) means any character(s) in this position, for example, ton. will find tons, tone and tonneau but not wanton because it has no following character.
/abc/ - Matches the first occurrence of the 'abc' string.
 /12/ - Matches the first occurrence of the '12' string.
/ab*c/ - Matches the first occurrence of the string that begins with 'a', followed by any number of 'b' and ends with 'c'. Possible strings: 'ac', 'abc', 'abbc', 'abbbc', ...
/ab+c/ - Matches the first occurrence of the string that begins with 'a', followed by at least one 'b' and ends with 'c'. Possible strings: 'abc', 'abbc', 'abbbc', ...
 /ab{2,}c/ - Matches the first occurrence of the string that begins with 'a', followed by two or more 'b' and ends with 'c'. Possible strings: 'abbc', 'abbbc', 'abbbbc', ...
/(ab)*c/ - Matches the first occurrence of the string that begins with some 'ab', followed by 'c'. Possible strings: 'c', 'abc', 'ababc', 'abababc', ...
/(ab)+c/ - Matches the first occurrence of the string that begins with one or more 'ab', followed by 'c'. Possible strings: 'abc', 'ababc', 'abababc', ...
/(ab){2,}c/ - Matches the first occurrence of the string that begins with two or more 'ab', followed by 'c'. Possible strings: 'ababc', 'abababc', 'ababababc', ...
/ab?c/ - Matches the first occurrence of the string that begins with 'a', followed by at most one 'b' and ends with 'c'. Possible strings: 'ac' 'abc'.
 /ab{1,2}c/ - Matches the first occurrence of the string that begins with 'a', followed by one or two 'b' and ends with 'c'. Possible strings: 'abc' 'abbc'.
 /ab{2}c/ - Matches the first occurrence of the 'abbc' string.
/(ab)?c/ - Matches the first occurrence of the string that begins at most one 'ab', followed by 'c'. Possible strings: 'c' 'abc'.
/(ab){1,2}c/ - Matches the first occurrence of the string that begins with one or two 'ab', followed by 'c'. Possible strings: 'abc' 'ababc'.
/(ab){2}c/ - Matches the first occurrence of the 'ababc' string.
/^whole$/ - Checks if the analyzed string is 'whole'.
 /^ca(t|r)$/ - Checks if the analyzed string is 'cat' or 'car'.
/[abc]/ - Matches the first occurrence of the 'a', 'b' or 'c' character.
 /[ab]{1,2}/ - Matches the first occurrence of the 'a', 'b', 'aa', 'ab', 'ba' or 'bb' string.
 /[^abc]/ - Matches the first occurrence of a character that is not 'a', 'b' and 'c'.
/\d/ - Matches the first occurrence of any digit.
/\d{1,2}/ - Matches the first occurrence of one or two digits. /\d|[1-9]\d/ - Matches the first occurrence of an integer between 0 and 99. Similar to the previous regular expression, but '00', '01', '02', ..., '09' are not allowed.
 /(\+|-)?(\d|[1-9]\d)/ - Matches the first occurrence of an integer between 0 and 99 with or without sign. Note that the '\' character needs to be used before the '+' character, because the '+' character has special meaning in regular expressions.
Several special characters can be used in a regular expression. With these characters various patterns can be specified.
The following table lists the supported special characters.
Character
Description
\
The backslash character has two different meanings, depending on the character that follows.
  • For an alphanumeric character, it indicates that it is a special character.
    For example, /d/ - matches a 'd' character, but /\d/ - matches a digit character.
    Some of the alphanumeric characters have no meaning together with the backslash character. Using the backslash character with these characters causes an error.
  • For a non-alphanumeric character, it indicates that it is a normal character.
    It can be useful if you need a pattern matching for a special character.
    For example, /a*/ - matches any number of 'a' character, but /a\*/- matches only the 'a*' string.
^
Indicates that the match must start at the beginning of the input string. If the multiline flag is specified, the match can start after a \r or \n character. For example, /^m/ has no match in 'woman', but has a match in 'man'
$
Indicates that the match must end at the end of the input string. If the multiline flag is specified, the match can end at a \r or \n character. For example, /m$/ has no match in 'format', but has a match in 'form'.
*
Matches the preceding item zero or more times. Equivalent to {0,}. For example, pattern: /b*a/g - input: 'hubba and bubba'.
+
Matches the preceding item at least one time. Equivalent to {1,}. For example, pattern: /b+a/g - input: 'hubba and bubba'.
?
Matches the preceding item zero or one time. Equivalent to {0,1}. For example, pattern: /b?a/g - input: 'hubba and bubba'.
.
(dot) Matches any single character except the newline characters (\n, \r, \u2028 or \u2029). Use the [\s\S] pattern to match any character including newline characters. For example, pattern: /.s/g - input: 'sail the seas'.
(pattern)
Matches pattern, and stores the match (called as capturing parentheses, captured subexpression or parenthesized subexpressions). The stored parts of a match can be retrieved through the $1, $2, ... , $9 properties.
(?:pattern)
Matches pattern, but does not store the match (called as non-capturing parentheses, non-captured subexpression or non-parenthesized subexpressions).
pattern1(?=pattern2)
Matches pattern1 only when pattern1 is followed by pattern2. For example, /a(?=b)/g matches 'a' letter only if the 'a' letter is followed by a 'b' letter. Pattern: /a(?=b)/g - input: 'abracadabra'.
pattern1(?!pattern2)
Matches pattern1 only when pattern1 is not followed by pattern2. For example, /a(!=b)/g matches 'a' letter only if the 'a' letter is not followed by a 'b' letter. Pattern: /a(!=b)/g - input: 'abracadabra'.
pattern1|pattern2
Matches pattern1 or pattern2. For example, pattern: /(a|r)/g - input: 'agreed'.
{n}
Where n is a nonnegative integer. Matches the preceding item exactly n times. For example, pattern: /9{2}/g - input: '79799799979999'.
{n,}
Where n is a nonnegative integer. Matches the preceding item at least n times. For example, pattern: /9{2,}/g - input: '79799799979999'.
{n,m}
Where n and m are nonnegative integers and n <= m. Matches the preceding item at least n and at most m times. For example, pattern: /9{2,3}/g - input: '79799799979999'.
[xyz]
A character set. Matches any one of the enclosed characters. For example, pattern: /[adr]/g - input: 'agreed'.
[^xyz]
A negated character set. Matches any character that is not enclosed. For example, pattern: /[^adr]/g - input: 'agreed'.
[a-z]
A range of characters. Matches any character in the specified range. For example, pattern: /[a-d]/g - input: 'agreed'.
[^a-z]
A negated range of characters. Matches any character that is not in the specified range. For example, pattern: /[^a-d]/g - input: 'agreed'.
\b
Matches a word boundary, not a backspace! The [\b] pattern matches a backspace. A word boundary can be at the beginning or end of a word, it is the position between a word and a non-word character (see the \w and \W patterns). For example, pattern: /\ba/g - input: 'an abracadabra'.
\B
Matches a non-word boundary. For example, pattern: /\Ba/g - input: 'an abracadabra'.
\cx
Where x is a character (letter) from A-Z or a-z. Matches the control character. For example, \cI matches Control-I (tab).
\d
Matches a digit. Equivalent to [0-9]. For example, pattern: /\d/g - input: 'a1b23c'.
\D
Matches a non-digit. Equivalent to [^0-9]. For example, pattern: /\D/g - input: 'a1b23c'.
\f
Matches a form-feed character. Equivalent to \x0c. (The form-feed character causes the printer to advance one page length or to the top of the next page.)
\n
Matches a newline character. Equivalent to \x0a.
\r
Matches a carriage return character. Equivalent to \x0d.
\s
Matches any white space character. For example, pattern: /s\sa/g - input: 'Thats all'.
\S
Matches any non-white space character. For example, pattern: /\Sa/g - input: 'Thats all'.
\t
Matches a tab character. Equivalent to \x09.
\v
Matches a vertical tab character. Equivalent to \x0b.
\w
Matches an alphanumeric (letter or number) character including underscore. Equivalent to [A-Za-z0-9_]. For example, pattern: /\w+/g - input: '5+23'.
\W
Matches a non-word (non-alphanumeric) character. Equivalent to [^A-Za-z0-9_]. For example, pattern: /\W+/g - input: '5+23'.
\n
n is a positive integer. If the entire regular pattern contains at least n captured subexpressions before (see (pattern)), then the \n is a back reference to the nth subexpression, else n must be an octal code (see \ooo). For example, pattern: /(\d+)\+\1/g - input: '23+12, 23+23, 12+12+23'.
\0
Matches a NULL character.
\ooo
Matches a character specified by its octal code ooo. For example (the code of the '+' character is \053), pattern: /\053/g - input: '23+12'.
\xhh
Matches a character specified by its hexadecimal code hh. For example (the code of the '+' character is \x2B), pattern: /\x2B/g - input: '23+12'.
\uhhhh
Matches a character specified by its Unicode code hhhh. For example (the code of the '+' character is \u002B), pattern: /\u002B/g - input: '23+12'.

Examples:
/a/

Mary had a little lamb.
And everywhere that Mary
went, the lamb was sure
to go.

/Mary/

Mary had a little lamb.
And everywhere that Mary
went, the lamb was sure
to go.

 
/.*/
 
Special characters must be escaped.*
 
/\.\*/
Special characters must be escaped.*
    
/^Mary/
 
Mary had a little lamb.
And everywhere that Mary
went, the lamb was sure
to go.
 
/Mary$/
 
Mary had a little lamb.
And everywhere that Mary
went, the lamb was sure
to go.
/.a/ 
 
Mary had a little lamb.
And everywhere that Mary
went, the lamb was sure
to go.

/(Mary)( )(had)/ 
 
Mary had a little lamb.
And everywhere that Mary
went, the lamb was sure
to go.
 
/[a-z]a/ 
 
Mary had a little lamb.
And everywhere that Mary
went, the lamb was sure
to go.
 
/[^a-z]a/ 
 
Mary had a little lamb.
And everywhere that Mary
went, the lamb was sure
to go.
 
 
/cat|dog|bird/
 
The pet store sold cats, dogs, and birds.
 
/=first|second=/
 
=first first= # =second second= # =first= # =second=
 
/(=)(first)|(second)(=)/
 
=first first= # =second second= # =first= # =second=
 
/=(first|second)=/
 
=first first= # =second second= # =first= # =second=
 
    
/@(=+=)*@/ 
 
Match with zero in the middle: @@
Subexpresion occurs, but...: @=+=ABC@
Lots of occurrences: @=+==+==+==+==+=@
Must repeat entire pattern: @=+==+=+==+=@
 
/A+B*C?D/
 
AAAD
ABBBBCD
BBBCD
ABCCD
AAABBBC
 
 
/a{5} b{,6} c{4,8}/
 
aaaaa bbbbb ccccc
aaa bbb ccc
aaaaa bbbbbbbbbbbbbb ccccc
 
/a+ b{3,} c?/
 
aaaaa bbbbb ccccc
aaa bbb ccc
aaaaa bbbbbbbbbbbbbb ccccc
 
/a{5} b{6,} c{4,8}/
 
aaaaa bbbbb ccccc
aaa bbb ccc
aaaaa bbbbbbbbbbbbbb ccccc
 
    
/(abc|xyz) \1/
 
jkl abc xyz
jkl xyz abc
jkl abc abc
jkl xyz xyz
 
/(abc|xyz) (abc|xyz)/
 
jkl abc xyz
jkl xyz abc
jkl abc abc
jkl xyz xyz
 
/th.*s/
 
-- I want to match the words that start
-- with 'th' and end with 's'.
this
thus
thistle
this line matches too much
 
/th[^s]*./
 
-- I want to match the words that start
-- with 'th' and end with 's'.
this
thus
thistle
this line matches too much
 
 



Bind Some Event to Element and trigger that function when click the element

Scenario: Suppose if we used same click event function in various web pages. if you want do some logic some page button for that need to re...