ArticleDirectory
- 8.10 positioning character and literal character
- 8.10.1 positioning character used for text Verification
8.10 positioning character and literal character
During verification, some delimiters are used to specify the position of a character to facilitate matching. At the same time, the metacharacters in the expression must be escaped so that they can be properly displayed only by turning them into original characters. This section describes the two applications.
8.10.1 positioning character used for text Verification
Specifies the position where the matching mode appears in the target string. For example, it can only appear at the beginning or end, which is very useful for text format verification. In a regular expression, there are the following operators.
1. Use ^ to match the start position of the target string
The match must start with the target string, and ^ must appear at the beginning of the expression. For example, "^ o" matches o in "OK", but does not match o in "hello. If the multiline attribute of the Regexp object instance is set, ^ matches the first line, that is, the position after "N" and "R.Code8.11 demonstrates the use of this character.
Match the first character in line 2 of the code ^: 8.11.htm
<HTML>
<Head> <title> first line matching character ^ </title>
<Body>
<H1> match the first line character ^
<Script language = "JavaScript">
<! --
VaR reg_expression =/^ ZT/; // use the first metacharacter of a row
VaR textstring = prompt ("Enter the string to be checked :","");
VaR result = reg_expression.test (textstring); // true is returned for matching. Otherwise, false is returned.
Document. Write ("<font size = '+ 1'> <B>" + Result + "<br> ");
If (result ){
Document. Write ("<B> Regular Expression/^ ZT/matching string" + textstring + ". <br> ");
}
Else {
Alert ("no matching found! ");
}
// -->
</SCRIPT>
</Body>
</Html>
Run the code section. A dialog box is displayed, asking the user to enter a string. If you enter zt129837op2ueop (as shown in 8.12) and click "OK", the corresponding content is displayed in the browser window, as shown in 8.13.
Figure 8.12 input character string zt129837op2ueop figure 8.13 matching successful
2. Use $ to match the end position of the target string
The match must occur at the end of the target string, and $ must appear at the end of the expression to act as a locator. For example, "O $" matches "O" in "hello", but does not match "O" in "OK. If the mutiline attribute of the Regexp object instance is set, $ also matches the end of the row, that is, it matches the position before "N" and "R. Code 8.12 demonstrates the use of this character.
Matching character at the end of line 3 in code $: 8.12.htm
<HTML>
<Head>
<Title> matching characters at the end of a line $ </title>
</Head>
<Body>
<H1> matching characters at the end of a line $
<Script language = "JavaScript">
<! --
VaR reg_expression =/ZT $ /;
VaR textstring = prompt ("Enter the string to be checked :","");
VaR result = reg_expression.test (textstring); // true is returned for matching. Otherwise, false is returned.
Document. Write ("<font size = '+ 1'> <B>" + Result + "<br> ");
If (result ){
Document. Write ("<B> Regular Expression/ZT $/match string" + textstring + ". <br> ");
}
Else {
Alert ("no matching found! ");
}
// -->
</SCRIPT>
</Body>
</Html>
Run the code section. A dialog box is displayed, asking the user to enter a string. If you enter qweriqupoiasdzt (as shown in 8.14) and click "OK", the corresponding content is displayed in the browser window, as shown in 8.15.
Figure 8.14 qweriqupoiasdzt
3. Match A Word boundary with "B"
"B contains the position between the word and space, and the start and end positions of the target string. For example, "er" B "matches" er "in" never OK ", but does not match" er "in" verb ". Code 8.13 demonstrates the use of this character.
Matching character at the end of line 3 in code $: 8.13.htm
<HTML>
</Head>
<Body>
<H1> Use metacharacters "B
<Script language = "JavaScript">
<! --
VaR reg_expression =/"bman" B /;
VaR textstring = prompt ("Enter the string to be checked :","");
VaR result = reg_expression.test (textstring); // true is returned for matching. Otherwise, false is returned.
Document. Write ("<font size = '+ 1'> <B>" + Result + "<br> ");
If (result ){
Document. Write ("<B> Regular Expression/" "bman" "B/matching string" + textstring + ". <br> ");
}
Else {
Alert ("no matching found! ");
}
// -->
</SCRIPT>
</Body>
</Html>
Run the code section. A dialog box is displayed, asking the user to enter a string. If you enter "Man Woman" (as shown in 8.16) and click "OK", the corresponding content is displayed in the browser window, as shown in 8.17.
Figure 8.16 input string "Man Woman" figure 8.17 matching successful
4. Use "B" to match non-word boundaries
For example, "er" B "matches" er "in" verb ", but does not match" er "in" never ".
8.10.2 escape special characters
Some metacharacters used in the expression do not represent the original literal meaning. to match these metacharacters with special meanings, you must use "" to escape these characters as the original literal characters. Escape characters include "$", "(", ")", "*", "+", and ". "," [","] ","?" , "", "/", "^", "{", "}", And "| ".
"" Is used to mark the next character as a special character, literal character, reverse reference, or octal escape character. Therefore, you must match the literal meaning of "", "is required.
One way to create a Regexp object instance is to nest expressions in a pair of "/". Therefore, in expression mode, "/" must also be escaped.