character | meaning |
\ |
As a conversion, usually the character after “\ “is not interpreted in its original meaning, such as /b/ to match the character “b”, when b is preceded by a backslant bar /\b/ to match the boundary of a word. – or – Restores regular expression function characters, such as “* “matching its metacharacter 0 or more times, /a*/ will match a, AA,aaa, with “\”, /a\*/ will only match “A *”. |
^ | Matches an input or the beginning of a line, /^a/ matches “an a “, but not “an a” |
$ | Matches An input or the end of a line, /a$/ matches “An a “, but not “An a” |
* | Matches the preceding metacharacter 0 or more times, and /ba*/ will match b,ba,baa,baaa |
+ | Matches the preceding metacharacter once or more, and /ba*/ will match ba,baa,baaa |
? | Matches the preceding metacharacter 0 or 1 times, and /ba*/ will match b,ba |
(x) | Match x save x in name $1… In the $9 variable |
x|y | Matches x or y |
{n} | N exact matches |
{n,} | Match n times or more |
{n,m} | Match the n – m times |
[xyz] | A character set that matches any character (or metacharacter) in the set |
[^xyz] | Does not match any character in this collection |
[\b] | Matches a backspace character |
\b | Matches the boundaries of a word |
\B | Matches the non-boundary of a word |
\cX | Here, X is a control character, /\cM/ matches Ctrl-m |
\d | Matches a word character, /\d/ = /[0-9]/ |
\D | Matches a non-alphanumeric character, /\D/ = /[^0-9]/ |
\n | Matches a newline character |
\r | Matches a carriage return |
\s | Matches a blank character, including \n,\r,\f,\t,\v, etc |
\S | Matches a non-whitespace character equal to /[^\n\f\r\t\v]/ |
\t | Matches a TAB character |
\v | Matches a rehash TAB character |
\w | Matches a alphanumeric character that can form a word, including an underscore, such as [\w] matches the 5 in “$5.98 “, equal to [A-za-z0-9] |
\W | Matching a character that cannot form a word, such as [\W] matching the $in “$5.98 “, is equal to [^ A-za-z0-9]. |
Re = new RegExp(“pattern “,[“flags “]) pattern: regular expression flags: g (full text search for all patterns) I (ignoring case) m (multi-line search) |
VaScript dynamic regular expression problem
Can regular expressions be generated dynamically? For example in JavaScript: var STR = “strTemp “; To generate: var re = /strTemp/; Var re = “/ “+ STR + “/” How to do that? |
|
[JAVA] Javascript regular expressions | |
|
[RED]function dateVerify(date){ var reg = /^(\d{4})(-)(\d{2})\2(\d{2})$/;
var r = date.match(reg); if(r==null) return false; var d= new Date(r[1], r[3]-1,r[4]); var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate();
date=r[1]+r[2]+((r[3]-1)+1)+r[2]+((r[4]-1)+1); return newStr==date;
}[/RED]
|
Regular expressions in JavaScript (2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Properties and methods of regular expression objects Predefined regular expressions have the following static properties: Input, Multiline, lastMatch, lastParen, leftContext, rightContext, and $1 through $9. Input and multiline can be pre-set. The values of other attributes are assigned different values depending on conditions after executing exec or test methods. Many properties have both long and short (Perl-style) names, and both names point to the same value. (JavaScript emulates Perl’s regular expressions) property of a regular expression object
Methods of regular expression objects
Example <script language = “JavaScript”> var myReg = /(w+)s(w+)/; var str = “John Smith”;
|
Javascript regular expression verification
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * EO_JSLib in js * javascript regular expression test * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / check whether all composed of digital function isDigit (s) { Var patrn = / ^ [0-9] {1, 20} $/; if (! Patrn. Exec (s)) return false return true} // Verify the login name. The login name can contain only 5 to 20 characters, including digits, underscores (_), and underscores (_), starting with a letter. The string function isRegisterUserName (s) {var patrn = / ^ {1} [a zA – Z] ([a zA – Z0-9] |. [_]) {4} 3 $/; if (! Patrn. Exec (s)) return false return true} Function isTrueName(s) {var patrn=/^[a-za-z]{1,30}$/; if (! Function isPasswd(s) {var patrn=/^(\w){6,20}$/; function isPasswd(s) {var patrn=/^(\w){6,20}$/; if (! Patrn. Exec (s)) return false return true} Can be “+” at the beginning, in addition to the digital, can contain a “-” function isTel (s) {/ / var patrn = / ^ [+] {0, 1} (\ d {1, 3} [])? ([-]? (\ d {1, 12}) + $/; Var patrn = / ^ [+] {0, 1} (\ d {1, 3} [])? ([-]? ((\ d) | []) {12} 1) + $/; if (! Patrn.exec (s)) return false return true} Must begin with Numbers, in addition to the digital, can contain a “-” function isMobil (s) {var patrn = / ^ [+] {0, 1} (\ d {1, 3} [])? ([-]? ((\ d) | []) {12} 1) + $/; if (! Function isPostalCode(s) {//var patrn=/^[a-za-z0-9]{3,12}$/; Var patrn = / ^ [a zA – Z0-9] {3, 12} $/; if (! Patrn. Exec (s)) return false return true} / / check search keyword function isSearch (s) {var patrn = / ^ [^ ` ~! @ # $% ^ & * () + = | \ \ \] [\] \ {\} :; ‘\,. < > /? ] {1} [^ ` ~! @ $% ^ & () + = | \ \ \] [\] \ {\} :; ‘\,. < >? ] {0} 3 $/; if (! } function isIP(s) {var patrn=/^[0-9.]{1,20}$/; if (! patrn.exec(s)) return false return true }
Regular expression regular expression
Regular expression is a regular expression. Regular expressions have a very powerful and complex object, RegExp, available in JavaScript1.2. A regular expression object is used to regulate a regular expression (that is, whether the expression conforms to certain requirements, such as an Email address format), and it has properties and methods for checking whether a given string conforms to the rules. In addition, the properties of individual regular expression objects that you create with the RegExp constructor already have the static properties of regular expression objects defined in advance, and you can use them at any time. Core objects: Available in JavaScript 1.2, NES 3.0 and above. The toSource method has been added to JavaScript since version 1.3. Method: text format or RegExp constructor function. The text creation format uses the following format: /pattern/flags that is, /pattern/flags
Constructor function methods: new RegExp(“pattern”[, “flags”])
Parameter: pattern Represents the text of the regular expression
If this is specified, flags can be one of the following values: G: global match I: ignore case gi: Both global match and Ignore case(match all possible values, also ignore case)
Note: Arguments in text format should not be quoted, while arguments in constructor functions should be quoted. So the following expression creates the same regular expression: /ab+c/ I new RegExp(“ab+c”, “I “)
Description: When using constructors, it is necessary to use normal strings to avoid the rule (adding the leading character \ to the string). For example, the following two statements are equivalent: re = new RegExp(“\\w+”) re = /\w+/
The following provides a complete list and description of the complete pairs of special characters that can be used in regular expressions.
Table 1.3: Special characters in regular expressions:
Character \ meaning: For characters, usually in a literal sense, indicating that the following character is a special character, \ is not explained. For example, the /b/ matches the character ‘b’, which becomes a special character by preceded by a backslash \, that is, /\b/, indicating the boundary between matching a word. Or: For several characters, the usual description is special, indicating that the character immediately following is not special but should be interpreted literally. For example, * is a special character that matches any character (including 0 characters). For example, /a*/ indicates that zero or more AS are matched. To match the literal *, precede the a with a backslash; For example: /a\*/ matches ‘a*’.
Character ^ Meaning: Indicates that the matching character must be first. For example: /^A/ does not match the ‘A’ in “an A,” but matches the first ‘A’ in “an A.”.
The $character meaning: similar to ^, matches the last character. For example: /t$/ does not match the ‘t’ in “Eater”, but matches the ‘t’ in “eat”.
Character * Meaning: Matches the character preceded by * 0 or n times. For example :/bo*/ matches ‘boooo’ in “A Ghost Booooed “or ‘b’ in “A bird Warbled”, but does not match any characters in “A goat g runted”.
Character + Meaning: Matches the character before the + sign once or n times. Equivalent to {1,}. For example: /a+/ matches ‘a’ in “candy” and all ‘a’ in “caaaaaaandy.”
Characters? Meaning: Match? The preceding character is 0 or 1 times. For example: / e? le? / matches ‘el’ in “angel” and ‘le’ in “Angle.”
Character. Meaning :(decimal point) matches all single characters except newline. For example: /.n/ matches ‘an’ and ‘on’ in “nay, an apple is on the tree”, but does not match ‘nay’.
The character (x) meaning: Matches ‘x’ and records the matched value. For example: /(foo)/ matches and records ‘foo’ in “foo bar.” Matching substrings can be matched by elements in the result array [1]… , [n] returns, or is the property of the RegExp object $1… , $9 returns.
Character x | y: matching ‘x’ or ‘y’. For example: / green | red/match “green apple” in the ‘green’ and “‘ red ‘. In the red apple.”
Meaning of the {n} character: where n is a positive integer. Matches the first n characters. For example: /a{2}/ does not match the ‘A’ in “candy,” but matches all ‘A’ in “caandy,” and the first two ‘a’ in “caaandy.”
Meaning of the character {n,} : where n is a positive integer. Matches at least n preceding characters. For example: /a{2,} does not match ‘a’ in “candy”, but matches all ‘A’ in “caandy” and all ‘A’ in “caaaaaaandy.”
Meaning: the characters {n,m} are both positive integers. Matches at least n characters and at most m characters. For example: /a{1,3}/ does not match any characters in “cndy”, but matches the ‘a’ in “candy,” the first two ‘a’ in “caandy,” and the first three ‘a’ in “caaaaaaandy”, note: Even though “caaaaaaandy” has a lot of ‘A’s, it only matches the first three’ A’s, “AAA”.
Character [xyz] meaning: A list of characters matching any character in the list. You can indicate a character range by hyphens. For example, [abcd] is the same as [a-c]. They match the ‘B’ in “brisket” and the ‘C’ in “ache”.
Character [^xyz] meaning: a character complement, that is, it matches everything except the listed characters. You can use a hyphen – to indicate a character range. For example: [^ ABC] and [^ a – c] equivalent, ‘r’ in their first match “brisket” and “chop.” the ‘h’.
Character [\b] meaning: matches a space (not to be confused with \b)
Character \b meaning: Matches the dividing line of a word, such as a space (not to be confused with [\b]) e.g. /\bn\w/ matches ‘no’ in “noonday”, /\wy\b/ matches ‘ly’ in “possibly yesterday.”
Character \B meaning: matches the nondividing line of a word e.g. /\w\Bn/ matches the ‘on’ in “noonday”, /y\B\w/ matches the ‘ye’ in “possibly yesterday.”
Character \cX meaning: here X is a control character. Matches the control character of a string. For example: /\cM/ matches control-m in a string.
Character \d meaning: matches a number, equivalent to [0-9]. For example: /\d/ or /[0-9]/ matches ‘2’ in “B2 is the suite number.”
Character \D meaning: matches any non-numeric, equivalent to [^0-9]. For example: /\D/ or /[^0-9]/ matches ‘B’ in “B2 is the suite number.”
Character \f meaning: Matches a form character
Character \n meaning: Matches a newline character
Character \r meaning: Matches a carriage return character
Meaning of character \s: Matches a single white space, including space, TAB, form feed, and newline, equivalent to [\f\n\r\t\v]. For example: /\s\w*/ matches’ bar’ in “foo bar.”
Character \S meaning: Matches a single character other than white space, equivalent to [^ \f\n\r\t\v]. For example: /\S/\w* matches ‘foo’ in “foo bar.”
Character \t meaning: Matches a TAB character
Character \v meaning: Matches a leading TAB character
Character \w meaning: Matches all numbers and letters as well as underscores, equivalent to [A-za-z0-9_]. For example: /\w/ matches ‘a’ in “apple,” ‘5’ in “$5.28,” and ‘3’ in “3D.”
Character \W meaning: Matches any character other than digits, letters, and underscores, equivalent to [^ A-za-z0-9_]. For example, /\W/ or /[^$a-za-z0-9_]/ matches the ‘%’ in “50%.”.
Character \n meaning: where n is a positive integer. Matches the value of n (counting left parentheses) of the last substring of a regular expression.
For example: /apple(,)\sorange\1/ matches the ‘apple, orange’ in “apple, orange, cherry, peach.” Here’s a more complete example. Note: if the number in the left parentheses is smaller than the number specified by \n, \n takes the octal escape of the next line as the description.
The meaning of the characters \ooctal and \xhex: here \ooctal is an octal escape value, while \xhex is a hexadecimal escape value that allows embedding ASCII codes in a regular expression.
Text symbols provide a way to edit regular expressions when they are checked. Regular expressions can be kept constant by means of literal symbols. For example, if you use literal symbols in a loop to construct a regular expression, the regular expression does not need to be compiled repeatedly. Regular expression object constructors, such as New RegExp(“ab+c”), provide run-time compilation of regular expressions. Constructors should be used when you know that the patterns of positive expressions change, or when you do not know the patterns of regular expressions and they are obtained from another source, such as user input. Once you have defined a regular expression that can be used anywhere and can be changed, you can compile a new regular expression to reuse using the compilation method. A separate pre-defined RegExp object can be used in each window; That is, each separate JavaScript thread runs to get its own RegExp object. Because each script is non-interruptible within a thread, this ensures that different scripts do not overwrite the value of the RegExp object. The predefined RegExp object contains static attributes: Input, Multiline, lastMatch,lastParen, leftContext, rightContext, and from $1 to $9. The input and multiline attributes can be preset. The values of other static attributes are set after executing the exec and test methods of individual regular expression objects, and after executing the match and replace methods of strings.
Properties Note that several properties of the RegExp object have both long and short names (like Perl). These names all refer to the same value. Perl is a programming language, and JavaScript mimics its regular expressions.
Properties of $1,… , $9 gets a matching substring, if any
Attribute $_ reference input
Attribute $* refer to multiline
Attribute $& refer to lastMatch
Attribute $+ refer to lastParen
The $’ attribute refers to leftContext
The $’ attribute refers to rightContext
The constructor property specifies that the object prototype function is created
The global attribute determines whether to test whether the regular expression does not match all strings, or simply conflicts with the first one.
The ignoreCase attribute determines whether case is ignored when attempting to match strings
Attribute INPUT Is the opposite string when the regular expression is matched.
The lastIndex attribute determines where the next match starts
Property lastMatch Last matched character
Attribute lastParen substring matches when the last parenthesized, if any.
The substring before the last match of the leftContext property.
Whether the property multiline is searched on multiple lines of a string.
The prototype attribute allows you to attach attributes to all objects
The substring of the rightContext property that was last matched.
Property source Schema text
The compile method compiles a regular expression object
The exec method runs regular expression matching
The test method tests regular access matching
The toSource method returns a literal description of the object specified; You can use this value to create a new object. Object. ToS ource methods are not considered.
The toString method returns a string describing the specified Object, regardless of object.toString.
The valueOf method returns the original valueOf the specified diagonal. The Object.valueof method is not considered.
In addition, this object inherits the object’s watch and unwatch methods
Example: Example 1. The following example script uses the replace method to convert words in a string. In the replacement text, the script uses the values of the $1 and $2 properties of the global RegExp object. Notice the name of the $property of the RegExp object when passed as the second argument to the replace method. < SCRIPT LANGUAGE = “JavaScript1.2” > re = / (\ w +) \ s +) (\ w /; str = “John Smith”; newstr=str.replace(re,”$2, $1″); Document. write(newstr) displays the result: “Smith, John”.
Example 2. In the following example script, regexp. input is set by the Change event handler. In the getInfo function, the exec method takes the value of regexp. input as its argument. Note that RegExp presets the $attribute.
Please enter your last name and age, and press Enter. <FORM><INPUT TYPE=”TEXT” NAME=”NameAge” onChange=”getInfo(this);” > < / FORM > < / HTML >
At $1,… The $9 attribute matches the substring in parentheses, if any. Is a property of RegExp static, read-only
In JavaScript 1.2, NES 3.0 and up, the description is provided: because input is a static property, it is not an attribute of an individual regular expression object. You can access this property using regexp. input.
There is no limit to the number of substrings that can be parenthetical, but regular expression objects can keep only the last nine. If you want to access all the matching strings in parentheses, you can use the returned array.
These attributes can be used to replace the string (output) with the regexp. replace method. When using this approach, you don’t have to worry about the RegExp object first. Examples are given below. When no parentheses are included in the regular expression, the script interprets $n literally. (where n is a positive integer).
For example, the following script uses the replace method to swap the positions of words in the string. In the replaced text string, the script uses the values of the $1 and $2 properties of the regular expression RegExp object. Note: When they pass arguments to the replace method, the name of the RegExp object for the $attribute is not taken into account. < SCRIPT LANGUAGE = “JavaScript1.2” > re = / (\ w +) \ s +) (\ w /; str = “John Smith”; newstr=str.replace(re,”$2, $1″); Document. write(newstr) The output is Smith, John.
Regular expression regular expression
Regular Expression Details (2)
For the following new objects that are not regular expressions see the corresponding JavaScript object property $_ property reference Input $* Property reference multiline $& property reference lastMatch $+ property reference lastParen $’ property reference leftContext $’ attribute reference rightContext compile method A method that compiles regular expression objects during a script run that belongs to RegExp provides syntax in JavaScript 1.2, NES 3.0 and above: Regexp.compile (pattern[, flags]) Number: regexp Specifies the name of the regular expression. The value can be a variable name or a string. The definition text of the pattern regular expression. Flags, if specified, can be one of the following: “g”: matches all possible strings. “I “: ignores case and “gi”: Matches all possible strings and ignores case description: Compile a regular expression using the compile method created with the RegExp constructor function. This forces the regular expression to be compiled once, rather than every time it is encountered. Use the compile method when you are sure that the regular expression will stay the same (after getting its match pattern), so you can use it multiple times in your script. You can also use the compile method to change regular expressions at run time. For example, if the regular expression changes, you can use the compile method to recompile the object to make it more efficient. Using this method changes the values of the source, global, and ignoreCasesource properties of the regular expression. Constructor refers to functions that prototype objects. Note that the value of this Property is provided by the function itself, not by a string containing RegExp’s name.property. The ECMA version of ECMA-262 is described in JavaScript 1.1, NES 2.0 and above: refer to the object.constructor. exec method to run a match search on the specified string. Returns an array of results. Exec ([STR]) RegExp ([STR]) Parameter: RegExp, the name of a regular expression, can be a variable name or a literal definition string. STR, the string to match the regular expression. If omitted, the value of regexp. input is used. Description: As in the syntax description, the regular expression exec method can be called directly (using regexp.exec(STR)) or indirectly (using regexp(STR)). If you’re just running to find a match, you can use the String search method. If the match is successful, the exec method returns an array and updates the value of the regular expression object property and the predefined regular expression object, RegExp. If the match fails, the exec method returns NULL. The following are the return values of the SCRIPT: Object property /Index Description Example myArray
MyArray contents [“dbBd”, “bB”, “d”] index 0 based match index 1 Input Original string cdbBdbsbz [0] Last matched character dbBd [1],… [n] Match string enclosed in parentheses, if any. There is no limit to the number of parentheses. [1] = bB [2] = d myRe lastIndex index value to start the next match 5 ignoreCase indicates whether “I” is used to ignoreCase true global indicates whether the “g” flag is used to match all possible strings true Source text string d(b+)(d) RegExp lastMatch$& last matched character dbBd leftContext$\Q last matched substring c rightContext$’ last matched substring BSBZ $1 . Matching substring in parentheses, if any. The number of parentheses is unlimited, but RegExp can keep only the last nine $1 = bB $2 = d lastParen $+ the last parenthes-matched substring, if any d
If your regular expression uses the “G” flag, you can use the exec method multiple times to match the same string consecutively. When you do this, the new match will start in the substring determined by the value of the lastIndex property of the regular expression. For example, suppose you use the following SCRIPT: