Đang chuẩn bị liên kết để tải về tài liệu:
Học JavaScript qua ví dụ part 86
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Neo đậu Metacharacters Thường thì nó là cần thiết để giữ chặt một metacharacter xuống, để nó phù hợp chỉ khi hình khác được tìm thấy ở đầu hoặc cuối của một dòng, từ, hay chuỗi. Những metacharacters được dựa trên một vị trí chỉ để trái hoặc bên phải của các nhân vật đang được xuất hiện. | 754 Chapter 17 Regular Expressions and Pattern Matching EXPLANATION 1 The variable called myString is assigned a string of lowercase letters just exactly like the last example. 2 The regular expression reads Search for one or more lowercase letters but after the sign there is a question mark. The question mark turns off the greed factor. Now instead of taking as many lowercase letters as it can this regular expression search stops after it finds the first lowercase character and then replaces that character with XXX. See Figure 17.27. Old stuns abcdefçlüjklmiiopqrstuvwxyz New string. XXXbcdefgluijkliiuiopqistuvwxyz Figure 17.27 This is not greedy Output from Example 17.25. 17.4.5 Anchoring Metacharacters Often it is necessary to anchor a metacharacter down so that it matches only if the pattern is found at the beginning or end of a line word or string. These metacharacters are based on a position just to the left or to the right of the character that is being matched. Anchors are technically called zero-width assertions because they correspond to positions not actual characters in a string for example Adocl will search for abc at the beginning of the line where the A represents a position not an actual character. See Table 17.10 for a list of anchoring metacharacters. Table 17.10 Anchors Assertions Metacharacter What It Matches a Matches to beginning of line or beginning of a string. Matches to end of line or end of a string. b Matches a word boundary when not inside . B Matches a nonword boundary. EXAMPLE 17.26 html head title title head body From the Library of WoweBook.Com 17.4 Getting Control The Metacharacters 755 EXAMPLE 17.26 continued script type text javascript 1 var reg_expression AWill Beginning of line anchor 2 var textString prompt Type a string of text 3 var result reg_expression.test textString Returns true or false document.write result br if result document.write b The regular expression AWill matched the string textstring . br else alert No Match