Getting started with JavaScript: Regular Expressions and javascript

Source: Internet
Author: User

Getting started with JavaScript: Regular Expressions and javascript

Regular Expression, also known as Regular Expression and Regular Expression (English: Regular Expression, often abbreviated as regex, regexp or RE in code), is a concept of computer science. Regular Expressions use a single string to describe and match a series of strings that conform to a certain syntax rule. In many text editors, regular expressions are usually used to retrieve and replace texts that match a certain pattern.


I. Create

Creating a regular expression is similar to creating a string. You can use either of the following methods.

Method 1: NEW operator

var box = new RegExp("Box", 'ig');

Method 2: literal

var box=new RegExp/box/ig;

Mode Parameters
Parameters Description
I Case Insensitive
G Global match
M Multi-row matching


Ii. Test

The RegExp object provides two test methods:


Method 1: test ()

Check whether a specified regular expression exists in the string. The returned value is a Boolean value. If the regular expression matches the regular expression, True is returned. If the regular expression does not exist, False is returned.

Method 2: exec ()

If the query is successful, an array containing information about the query string is returned. If a failure occurs, NULL is returned.



Instance 1:

The matching information is box, and the I parameter is case-insensitive. Search for the string in which the str variable is stored. The returned variable is True.

var pattern = new RegExp('box', 'i');var str = 'This is a Box!';alert(pattern.test(str));



Instance 2:

The content in the matched source string is returned using the exec () method. If no matching is found, NULL is returned.

           var pattern = new RegExp('box', 'i');           var str = 'This is a Box!';                   alert(pattern.exec(str));


3. Other methods:



1. match () method

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

Return Value: Box, box


2. replace () method

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

Returned value: This is a big Apple! That is a beautiful Apple



3. search () method

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


The returned result is: Found location. If not,-1 is returned.


4. split () method

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


Returns the string array to be split. This is a big ,! That is a beautiful,



Iv. Access Control


Regular Expression metacharacters are characters with special meanings. They have some special functions and can change the matching mode. Make a simple example.



Instance:

Simple email Verification:

           var pa = /^([\w\.\-]+)@([\w\-]+)\.([\w]{2,4})$/;           var str = 'zhou@163.com';           alert(pa.test(str));
() Is used in grouping mode. {2, 4} indicates matching strings in the group for 2 to 4 times.


Greedy and inert regular quantifiers:




Greedy quantizer: first check whether the entire string matches. If not, remove the last character for matching. If not, continue to remove the last character, guides you to find a match or stop without any characters.
 
Inertia quantizer: first check whether the first string matches. If the first string does not match, add the second string. So on, greedy quantifiers are the opposite of greedy quantifiers.


Instance 1:

? No. The greedy match is disabled, and only the first character a in the string is replaced.

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

The returned value is YYYbcdefghijklmnopqrstuvwxyz.



Example 2:

If the g parameter is enabled globally, greed is disabled.

           var pattern = /8(.+?)8/g;           var str = 'This is 8google8,That is 8google8,There is 8google8 ';           var result = str.replace(pattern, '<strong>$1</strong>');           document.write(result);


V. Summary:


Assume that the user needs to write the name, age, gender, and e-mail when entering the HTML form. before submitting the form to the server, verify it at the front end. Check whether the information entered by the user meets the requirements. This client verification method can save a lot of server system resources for a better user experience.



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.