The regular expression of JS

Source: Internet
Author: User
Tags alphabetic character

Regular expressions (regular expression) is an object that describes the character pattern, and JS's RegExp class represents regular expressions, and both string and RegExp define methods to manipulate regular expressions, such as pattern matching, text retrieval, and substitution.

the definition of a regular expression:Defining a regular expression you can use the RegExp () constructor to create an RegExp object. However, the most common form of creation of REGEXP objects is implemented by direct volume, just like creating a String object: var reg =/s$/; the equivalent regular expression can also be constructed with the REGEXP () constructor: var reg = new RegExp ("S $"); The regular expression pattern rule is that there is a sequence of characters, including numbers and letters, and most of the characters are matched by literal meanings. For example, regular expressions:/java/can match any string containing a Java substring. In addition, regular expressions have many characters that represent special meanings that you need to keep in mind. For example:/s$/contains two characters, the first character s matches the literal meaning, the second character is a meta character with a special meaning, and is used to match the end of the string, so the meaning of the sentence is to match a string ending with S. Direct Volume characters:All the letters and numbers of the regular expressions mentioned above are matched by literal meanings. The same JS regular expression also supports non-alphabetic character matching. These characters need to be escaped by a backslash (\). For example, the escape character \ n is used to match line breaks. If we want to match these symbols in regular expressions. Then the prefix "\" must be used, which is a common rule. Other punctuation does not have a special meaning, and matches the literal meaning of other characters as well. character class:The character set is formed by placing direct volume characters in square brackets alone. A character set can match any of the characters it contains. As a result, the regular expression/[a,b,c]/matches any of the letters "a", "B", and "C". Repeat: Select, Group, reference: Specify a matching location: modifier: string method for matching pattern:String supports 4 regular expression methods, the simplest of which is search (), and his argument is a regular expression. Returns the starting position of the first substring to match, or 1 if the starting position is not found. If the parameter of search is not a regular expression, it is first converted to a regular expression through the RegExp () constructor. Search () does not support global indexing because he ignores g in regular expressions. Math () Function:
functionGetre () {varurl =/(\w+): \/\/([\w.] +) \/(\s*)/; varText = "Visit my blog at Http://www.log.com/~liwei"; varresult =text.match (URL); if(result!=NULL)            {                varFull = Result[0]; varprotocol = result[1]; varHost = Result[2]; }        }

EXEC () function

function Test ()        {            var pattern =/java/g;             var text = "JavaScript is more fun than java!" ;             var result;              while NULL             {                alert ("matched" +result[0]+ "," + "at position" +result.index+ "; Next search begins at" +  Pattern.lastindex);            }        }

The regular expression of JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.