References:
Syntax:
<string>.search(regexp)
Return: -1 = not found, 0=first position.
Example 1: click here - finds the first occurrence of the chars 'or'.
strText = 'Hello World'
alert(strText.search(/or/i)); //Look for the first occurrance of 'or'
//Returns: 7
Example 2: click here - identical to Example 1 just using the new RegExp() to create the expression object.
var reSearch = new RegExp("or","i");
strText = 'Hello World'
alert(strText.search(reSearch)); //Look for the first occurrance of 'or' (Same
as Example 1)
//Returns: 7
Example 3: click here - find words
enclosed by brackets.
strText = 'test ["a","b","c"][tag]'
//Look for the '[' but must be followed by a lower or upper case letter, then
any number of char(s) followed by a ']'.
alert(strText.search(/\[([a-z]|[A-Z]).*?([a-z]|[A-Z])\]/));
//Returns = 18
Syntax:
RegExp(pattern [, flags])
/pattern/flags
Creates a regular expression object for matching text according to a pattern.
Reference:
If specified, flags can have any combination of the following values:
Character | Meaning |
\ |
For characters that are usually treated literally, indicates that
the next character is special and not to be interpreted literally. For
example, or For characters that are usually treated specially, indicates that the next character is not special and should be interpreted literally. For example, * is a special character that means 0 or more
occurrences of the preceding character should be matched; for example,
|
^ |
Matches beginning of input. If the multiline flag is set to true,
also matches immediately after a line break character. For example,
|
$ |
Matches end of input. If the multiline flag is set to true, also
matches immediately before a line break character. For example, |
* |
Matches the preceding item 0 or more times. For example, |
+ |
Matches the preceding item 1 or more times. Equivalent to {1,} .
For example, |
? |
Matches the preceding item 0 or 1 time. For example, If used immediately after any of the quantifiers Also used in lookahead assertions, described under |
. |
(The decimal point) matches any single character except the newline
characters: \n \r \u2028 or \u2029. ([\s\S] can be used to
match any character including newlines.)For example, |
(x) |
Matches x and remembers the match. These are
called capturing parentheses.For example, |
(?:x) |
Matches x but does not remember the match.
These are called non-capturing parentheses. The matched substring can
not be recalled from the resulting array's elements [1], ..., [n]
or from the predefined RegExp object's properties $1,
..., $9 . |
x(?=y) |
Matches x only if x is
followed by y . For example, /Jack(?=Sprat)/
matches 'Jack' only if it is followed by 'Sprat'. /Jack(?=Sprat|Frost)/
matches 'Jack' only if it is followed by 'Sprat' or 'Frost'. However,
neither 'Sprat' nor 'Frost' is part of the match results. |
x(?!y) |
Matches x only if x is
not followed by y . For example, /\d+(?!\.)/
matches a number only if it is not followed by a decimal point.
|
x|y |
Matches either x or y .
For example, |
{n} |
Where n is a positive integer. Matches exactly
n occurrences of the preceding item.For
example, |
{n,} |
Where n is a positive integer. Matches at
least n occurrences of the preceding item.For
example, |
{n,m} |
Where n and m are
positive integers. Matches at least n and at most
m occurrences of the preceding item.For
example, |
[xyz] |
A character set. Matches any one of the enclosed characters. You can
specify a range of characters by using a hyphen. For example, |
[^xyz] |
A negated or complemented character set. That is, it matches
anything that is not enclosed in the brackets. You can specify a range
of characters by using a hyphen. For example, |
[\b] |
Matches a backspace. (Not to be confused with \b .) |
\b |
Matches a word boundary, such as a space. (Not to be confused with
[\b] .)For example, |
\B |
Matches a non-word boundary. For example, |
\cX |
Where X is a letter from A - Z. Matches a
control character in a string.For example, |
\d |
Matches a digit character in the basic Latin alphabet. Equivalent to
[0-9] .Note: In Firefox 2 and earlier, matches a digit character from any alphabet. ( bug 378738) For example, |
\D |
Matches any non-digit character in the basic Latin alphabet.
Equivalent to [^0-9] .Note: In Firefox 2 and earlier, all alphabet. ( bug 378738) For example, |
\f |
Matches a form-feed. |
\n |
Matches a linefeed. |
\r |
Matches a carriage return. |
\s |
Matches a single white space character, including space, tab, form
feed, line feed and other unicode spaces.equivalent_s
For example, |
\S |
Matches a single character other than white space.equivalent_S
For example, |
\t |
Matches a tab. |
\v |
Matches a vertical tab. |
\w |
Matches any alphanumeric character from the basic Latin alphabet,
including the underscore. Equivalent to [A-Za-z0-9_] .For
example, |
\W |
Matches any character that is not a word character from the basic
Latin alphabet. Equivalent to [^A-Za-z0-9_] .For example,
|
\n |
Where n is a positive integer. A back
reference to the last substring matching the n parenthetical in the
regular expression (counting left parentheses).For example, |
\0 |
Matches a NUL character. Do not follow this with another digit. |
\xhh |
Matches the character with the code hh (two
hexadecimal digits) |
\uhhhh |
Matches the character with the Unicode value hhhh
(four hexadecimal digits). |