Regular expressions for JavaScript introductory articles

Source: Internet
Author: User
Tags first string html form

Regular expressions, also known as formal notation, conventional notation (English: Regular expression, often abbreviated in code as regex, RegExp, or re), is a concept of computer science. A regular expression uses a single string to describe and match a series of strings that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern.


First, create  

There are two ways to create a regular expression and create a string similar to the following.

Method One: New operator

var box = new RegExp ("box", ' IG ');

Method Two: Literal

var box=new regexp/box/ig;

Mode parameters
Parameters Meaning
I Ignore case
G Global match
M Multi-line matching


second, the test 

The RegExp object provides two test methods:


Method one: Test ()

Finds whether the specified regular expression exists in the string, returns a Boolean value, returns True if it matches, and returns false if it does not exist.

Method two: Exec ()

If successful, returns an array of related information that contains the lookup string. If the failure returns NULL.



Example 1:

The match information for the Box,i parameter is case insensitive. Found in the string stored by the STR variable, the returned variable is true.

var pattern = new RegExp (' box ', ' I '); var str = ' This is a box! '; Alert (Pattern.test (str));



Example 2:

The Exec () method returns the contents of the matching source string and returns null if no match is made.

           var pattern = new RegExp (' box ', ' I ');           var str = ' This is a box! ';                   Alert (pattern.exec (str));


Third, other methods:  



1. Match () method

           var pattern =/box/ig;           var str = ' This is a big box! That's a beautiful box ';           Alert (Str.match (pattern));

Returned as: Box,box


2. Replace () method

           var pattern =/box/ig;           var str = ' This is a big box! That's a beautiful box ';           Alert (str.replace (pattern, ' Apple '));

Returned as: This is a big apple! That's a beautiful Apple



3. Search () method

           var pattern =/box/ig;           var str = ' This is a big box! That's a beautiful box ';           Alert (Str.search (pattern));


Returned as: The location found, if not found on the return-1.


4. Split () method

           var pattern =/box/ig;           var str = ' This is a big box! That's a beautiful box ';           Alert (Str.split (pattern));


Returned as: The string array to split into. This is a big,! That's a beautiful,



Iv. access Control 


Regular expression metacharacters are characters that contain special meanings, and they have some special functions that can change the way the pattern is matched. Make a simple example.



Instance:

Simple Email Verification:

           var pa =/^ ([\w\.\-]+) @ ([\w\-]+) \. ([\w]{2,4}) $/;           var str = ' [email protected] ';           Alert (Pa.test (str));
The function of () is the grouping mode, {2,4} means to match the string in the grouping 2-4 times.


The greed and inertia of regular quantifiers:




Greedy quantifiers: First see whether the entire string matches, if not match the last character is removed in the match, the mismatch continues to remove the last character, guide to find a match or no word characters stop.

Lazy quantifiers: First to see whether the first string matches, if the first mismatch in the second string is added and so on, the guide to find a match or no word characters stop, greedy quantifiers and greedy quantifier method is just the opposite.


Example one:

? A greedy match is turned off, replacing only the first character a in the string.

           var pattern =/[a-z]+?/;           var str = ' abcdefghijklmnopqrstuvwxyz ';           var result = Str.replace (pattern, ' YYY ');           alert (result);

The return value is; yyybcdefghijklmnopqrstuvwxyz



Example two:

The G parameter is turned on globally, and the greedy is forbidden.

           var pattern =/8 (. +?) 8/ G;           var str = ' This is 8google8,that are 8google8,there is 8google8 ';           var result = Str.replace (pattern, ' <strong>$1</strong> ');           document.write (result);


Five, Summary: 


Suppose the user fills in the HTML form, needs to write the name, the age, the gender, the e-mail and so on, before submits to the server, before the front end verifies again. See if the information entered by the user conforms to the rules. This method of client-side verification can save a lot of server system resources and get a better user experience.



Regular expressions for JavaScript introductory articles

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.