tailieunhanh - Học JavaScript qua ví dụ part 86

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 . Old stuns abcdefçlüjklmiiopqrstuvwxyz New string. XXXbcdefgluijkliiuiopqistuvwxyz Figure This is not greedy Output from Example . 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 for a list of anchoring metacharacters. Table 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 html head title title head body From the Library of Getting Control The Metacharacters 755 EXAMPLE 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 textString Returns true or false result br if result b The regular expression AWill matched the string textstring . br else alert No Match

TỪ KHÓA LIÊN QUAN