Regular Expressions in JavaScript

Source: Internet
Author: User

Regular Expressions in JavaScript

The regular expression (regex) is a logical formula for string operations. It uses the predefined characters and combinations of these characters, A "rule string", which is used to express a filtering logic for strings.Regular Expressions are mainly used to verify the input data of the client..

Given a regular expression and another string, we can achieve the following goals:

1. Whether the given string conforms to the filtering logic of the regular expression (called "match ");

2. You can use a regular expression to obtain the desired part from the string.

 

 

1. Create a regular expression:

 

1. new operator

 

Var box = new RegExp ('box'); // The first parameter string var box = new RegExp ('box', 'ig '); // optional mode modifier for the second parameter

 


 

2. Literal

 

Var box =/box/; // directly use two backslash var box =/box/ig; // Add the pattern modifier after the second slash

 

 

Ii. Test the regular expression:

 

The RegExp object contains two methods: test () and exec ()

1. test ()

The test () method finds whether a specified regular expression exists in the string and returns a Boolean value. If yes, true is returned. If no regular expression exists, false is returned.

 
<Script type = "text/javascript"> var box = new RegExp ("e"); document. write (box. test ("The best things in life are free"); </script> 

 



2. exec ()

 

The exec () method is also used to find the specified Regular Expression in the string. If the exec () method is successfully executed, an array containing the relevant information of the query string is returned. If the execution fails, null is returned.

 

 
<Script type = "text/javascript"> var box = new RegExp ("e"); document.write(box.exe c ("The best things in life are free"); </script> 

 

 

Iii. Regular Expression Methods in String objects

 

 

<Script language = "javascript" type = "text/javascript"> var str = "Windows is an excellent system. I like Windows! "; Alert (str); </script>

 

 

1. match

 

<Script language = "javascript" type = "text/javascript"> var str = "Windows is an excellent system. I like Windows! "; // Define the variable reg var reg =/Windows/g;/* var tmp = str. replace (reg, "Linux"); alert (tmp); */var found = str. match (reg); alert (found); </script>

 

 

2. replace

 

<Script language = "javascript" type = "text/javascript"> var str = "Windows is an excellent system. I like Windows! "; // Define the variable reg var reg =/Windows/g; var tmp = str. replace (reg," Linux "); alert (tmp); </script>

 


4. Greed and inertia:
Greedy: first match the entire string, and then from the right to the left. If it does not match,...
Inertia: match from left to right. If the first one does not match, add the second one...

Var pattern =/[a-z] +? /;/? The first var str = 'abcdefjhijklmnopqrstuvwxy'; var result = str. replace (pattern, 'xxx'); alert (result );


Two methods to prohibit greed:
Var pattern =/8 (. + ?) 8/g; // greedy is disabled. The enabled global var str = 'this is 8google8, That is 8google8, There is 8google8'; var result = str. replace (pattern ,'$1'); Document. write (result );

Var pattern =/8 ([^ 8] *) 8/g; // another type of greedy var str = 'this is 8google8, That is 8google8, There is 8google8 '; var result = str. replace (pattern ,'$1'); Document. write (result );

Summary:
Common regular expressions include checking the zip code, deleting unnecessary spaces, and simple email verification. Regular Expressions are flexible, logical, and functional. They can quickly achieve complex String Control in an extremely simple way, while saving a lot of system resources on the server, and provide 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.