Overview of JavaScript Regular Expressions _ Create methods and properties

Source: Internet
Author: User
Tags first string html form

Suppose the user needs to fill in the HTML form with name, address, date of birth, etc.

Then, before submitting the form to the server for further processing, the JavaScript program examines the form to confirm that the user has actually entered the information and that the information is compliant.

First, what is a regular expression:
Regular expressions (regular expression) are an object that describes the character pattern.

JavaScript's REGEXP ( abbreviated regular expression ) class represents regular expressions, while both String and REGEXP define powerful pattern-matching and text-retrieval and substitution functions using regular expressions.

Regular expressions are primarily used to validate the input data of the client.

When you retrieve a text, you can use a pattern to describe what you want to retrieve.

REGEXP is this pattern. The simple pattern can be a single character. More complex patterns include more characters and can be used for parsing, format checking, substitution, and so on.

You can specify the location of the search in a string, the type of character to retrieve, and so on. After the user fills out the form by clicking the button, the form is sent to the server, which is usually further processed by server scripts such as PHP and ASP.

Because of client authentication, you can save a lot of server-side system resources and provide a better user experience.

Second, create regular expressions: similar to creating a regular expression and creating a string, creating a regular expression provides two methods, one with the new operator and the other in the literal way.

  1. Create method One: use the new operator

    var New // first argument string    alert (box); // The result is that/box/    two backslashes are literal representations    of regular expressions var New // second parameter optional pattern modifier    alert (box); // /result is box/gi

  2, the creation of two: the use of the literal way (this method is more commonly used)

    var // Direct use of    two backslashes alert (box); // The result is/box/    . var // Add a pattern modifier    after the second slash alert (box); //   /box/ig


Third, test the regular expression:

1.   The RegExp object consists of two methods :

Test () and exec () are basically similar functions for testing string matches.

The test () method looks in the string for the presence of the specified regular expression and returns a Boolean value that returns true if it exists, or false if it does not exist.

The exec () method is also used to find the specified regular expression in a string, and if the Exec () method succeeds, returns an array of related information containing the lookup string. If execution fails, NULL is returned.

/*example of the test method using the new operator*/varPattern =NewRegExp (' box ', ' I ');//Create regular mode, case insensitivevarstr = ' This is a box! ';//Create a string to compare toAlert (Pattern.test (str));//Verify that the match is matched by the test () method/*example of a test method using a literal method*/varpattern =/box/i;//Create regular mode, case insensitivevarstr = ' This is a box! '; alert (Pattern.test (str));/*using a statement to implement a regular match*/Alert (/box/i.test (' This is a box! '));//mode and string replaced two variables, this method is not recommended/*returning a matching array using exec*/varPattern =/box/i;varstr = ' This is a box! '; alert (pattern.exec (str)); //matches the return array (Box) type is object, otherwise returns null

Regular expression methods that use strings: In addition to the test () and Exec () methods, the String object provides 4 methods for using regular expressions.

  

/ *get a matching array using the match method*/    varpattern =/box/ig;//Global Search (returns an array if there is no global match to the first string), case insensitive    varstr = ' This was a box!,that is a Box too ';                                 Alert (Str.match (pattern)); //match to two x Box,boxAlert (Str.match (pattern). length);//gets the length of the array        /*use Search to find matching data*/    varpattern =/box/i;//because the search method finds the return, which means no G global    varstr = ' This was a box!,that is a Box too ';                             Alert (Str.search (pattern)); //Find, return the first match to the position, otherwise return-1        /*Replace matching data with replace*/        varpattern =/box/ig;//If there is no global, match to the first, then the back does not match    varstr = ' This was a box!,that is a Box too '; Alert (str.replace (pattern,' Tom ');//replaced Box with Tom        /*split into string arrays using split*/    varpattern =//ig;varstr = ' This was a box!,that is a Box too ';                                 Alert (Str.split (pattern)); //split spaces into arraysAlert (Str.split (pattern). length);

  

  2. Static properties of the RegExp object:

Static properties can be called directly without a declaration (these things have no eggs)

    

Opera does not support input, lastmatch, Lastparen, and Multiline properties. IE does not support the Multiline property.

/ *using static properties*/    varPattern =/(g) oogle/; varstr = ' This is google! ‘;                        Pattern.test (str); //must be executed, static properties will not take effectalert (regexp.input);//prints out the currently matched string: This is google! alert (Regexp.leftcontext);//prints a string that matches the preceding string: this isalert (Regexp.rightcontext);//print out the string that matches the following string:! alert (Regexp.lastmatch);//Googlealert (Regexp.lastparen);//galert (regexp.multiline);//false

All properties can be manipulated using short names such as: Regexp.input can be rewritten as regexp[' $_ '], and so on. But Regexp.input is more special, it can also be written as regexp.$_.

  3. Instance properties of the RegExp object :

Instance properties need to be declared to be created before they can be used (these things have no eggs)

·

     /*using Instance Properties*/    varPattern =/google/IG;             alert (Pattern.global); //true, is it global?alert (pattern.ignorecase);//True if the case is ignoredalert (pattern.multiline);//false, whether line wrapping is supportedalert (Pattern.lastindex);//0, next match locationalert (Pattern.source);//Google, the source string for regular expressions        varPattern =/google/G; varstr = ' Google google google ';           Pattern.test (str); //Google, match first timealert (Pattern.lastindex);//6, location of second match

LastIndex IE and other browsers are biased in obtaining the next match, mainly on non-global matches. LastIndex also supports manual setting, direct assignment operation (pattern.lastindex= value).

Overview of JavaScript Regular Expressions _ Create methods and properties

Related Article

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.