JavaScript Tutorial/Regular Expressions/Introduction

Материал из Web эксперт
Перейти к: навигация, поиск

All the characters that require a backslash character to be taken literally within a pattern.

Character Description \f Form feed \n Newline \r Carriage return \t Tab \v Vertical tab \/ Forward slash (/) \\ Backward slash (\) \. Period (.) \* Asterisk (*) \+ Plus (+) \? Question Mark (?) \| Horizontal bar (|) \( Left parenthesis ( \) Right parenthesis ) \[ Left bracket ([) \] Right bracket (]) \{ Left curly brace ({) \} Right curly brace (}) \XXX ASCII character represented by the octal number XXX \xHH ASCII character represented by the hexadecimal number HH \cX The control character represented by X

Defining Patterns

There are special characters for creating almost any pattern.

Special Pattern Matching Characters

Character Description \w Matches any word character (alphanumeric). \W Matches any non-word character. \s Matches any whitespace character (tab, newline, carriage return, form feed, vertical tab). \S Matches any non-whitespace character. \d Matches any numerical digit. \D Matches any character that is not a number. [\b] Matches a backspace. . Matches any character except a newline. [...] Matches any one character within the brackets. [^...] Matches any one character not within the brackets. [x-y] Matches any character in the range of x to y. [^x-y] Matches any character not in the range of x to y. {x,y} Matches the previous item at least x times but not to exceed y times. {x,} Matches the previous item at least x times. {x} Matches the previous item exactly x times. ? Matches the previous item once or not at all. + Matches the previous item at least once.

Matches the previous item any number of times or not at all. | Matches the expression to the left or the right of the | character. (...) Group everything inside parentheses into a subpattern. \x Matches the same characters that resulted from the subpattern in group number x. Groups, which are designated with parentheses, are numbered from left to right. ^ Matches the beginning of the string or beginning of a line, in multiline matches. $ Matches the end of the string or end of a line, in multiline matches. \b Matches the position between a word character and a non-word character. \B Matches the position that is not between a word character and a non-word character.

Pattern Attributes

Character Description g Global match. Find all possible matches. i Make matching case-insensitive.

Pattern Matching

JavaScript uses the RegExp (short for Regular Expression) object to handle pattern matching.

The RegExp object can be created in two different ways.

The first way is to use the RegExp constructor and the keyword new:



   <source lang="javascript">

var lastName = new RegExp("ABC");</source>


Testing for Pattern Matches

The pattern matching methods in the String object require RegExp objects.

Pattern Matching Methods in the String Object

Method Description match(regExpObj) Searches for regExpObj pattern in string and returns result. replace(reqExpObj,str) Replaces all occurrences of the regExpObj pattern with str. search(reqExpObj) Returns the position of matching regExpObj pattern within the string. split(regExpObj,max) Splits string everywhere there is a matching regExpObj pattern up to max splits. The substrings are returned in an array.

The pattern matching methods in the RegExp object require String objects.

Pattern Matching Methods in the RegExp Object

Method Description exec(str) Searches for pattern in str and returns result test(str) Searches for pattern in str and returns true if match found, otherwise false is returned (str) Same as exec(str) method



   <source lang="javascript">

<html> <SCRIPT LANGUAGE="JavaScript">

</SCRIPT> </html></source>