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

Metasymbols Metasymbols cung cấp một cách khác để đại diện cho một lớp nhân vật. Ví dụ, thay vì đại diện một số là [0-9], nó có thể được biểu diễn như là \ d, và thay thế cho đại diện-resenting một nonnumber [^ 0-9] là \ D. Metasymbols được dễ dàng hơn để sử dụng và để loại hơn metacharacters | Getting Control The Metacharacters 741 EXAMPLE continued script type text javascript Negation within a Character Class 1 var reg_expression A0-9 2 var textString prompt Type a string of text 3 var result textString Returns true or false result br if result b The reg_ex A0-9 matched the string textstring . br else alert No Match script body html EXPLANATION 1 The caret inside a character class when it is the first character after the opening bracket creates a negation meaning any character not in this range. This regular expression matches a string that does not contain a number between 0 and 9. 2 User input is assigned to the variable textString. In this example abc was entered. 3 The regular expression test method will return true because the string abc does not contain a character ranging from 0 to 9 see Figure . Figure The user entered abc. It contains a character that is not in the range between 0 and 9. Metasymbols Metasymbols offer an alternative way to represent a character class. For example instead of representing a number as 0-9 it can be represented as d and the alternative for representing a nonnumber A0-9 is D. Metasymbols are easier to use and to type than metacharacters. From the Library of 742 Chapter 17 Regular Expressions and Pattern Matching Table Metasymbols Symbol What It Matches Character Class d One digit 0-9 D One nondigit A0-9 s One whitespace character tab space newline carriage return formfeed vertical tab S One nonspace character w One word character A-Za-z0-9_ W One nonword character AA-Za-z0-9 EXAMPLE html head title The Digit Meta Symbol title head body script type text javascript 1 var reg_expression 6 d d 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 6 d d matched the string textstring . br .

TỪ KHÓA LIÊN QUAN