Basic knowledge about javascript Regular Expressions: javascript Regular Expressions

Source: Internet
Author: User

Basic knowledge about javascript Regular Expressions: javascript Regular Expressions

Where are the benefits of regular expressions? Let's take a look at the following:
We use the string processing method in js to write the function to retrieve the numbers in the string:

 var str='dgh6a567sdo23ujaloo932';   function getNumber(obj){     var arr=[];     for (var i = 0; i < obj.length; i++) {       if (obj.charAt(i)>='0'&&obj.charAt(i)<='9'){           arr.push(obj.charAt(i));         }     }     return arr;   };   console.log(getNumber(str));  //["6", "5", "6", "7", "2", "3", "9", "3", "2"]

In the above method, we retrieve the numbers in the string, but we are not satisfied. What we need is in the form of ['6', '123', '23', '123, transform functions:

Function getNumber (obj) {var arr = []; var temp = ''; for (var I = 0; I <obj. length; I ++) {if (obj. charAt (I)> = '0' & obj. charAt (I) <= '9') {temp + = obj. charAt (I); // connect adjacent numbers} else {// execute if (temp) {arr. push (temp); temp = '';}};} if (temp) {// The role here is to display the last number. The reason is not to explain arr. push (temp); temp = '';} return arr ;};

Then we use a regular expression to solve the functions implemented by this function:

function getNumber2(obj){    var arr=[];    var re=/\d+/g;    arr.push(obj.match(re));    return arr;  };

Let's take a full look at the running results of the program:

<! DOCTYPE> 

From the above example, we can see that the regular expression method has the same effect, but the code is shorter and more efficient. This is the benefit of regular expressions.
Regular Expressions are generated to process strings more efficiently. They are the same as string processing methods, but more efficient and concise (regular expressions can only process strings)

Next, let's take a look at some common Regular Expressions:

Before that, let's talk about the regular expression. Regular Expressions are the same as other objects, such as array (), object (), and Date (). They all have initialization methods.
Var re =/the Matching content should be written here. If not written, it means watching the symbol/; // This is the simple creation of the regular object, I will use it directly in subsequent articles.
Var re = new RegExp (); // This creation method is also acceptable. You know, but what is different from simple writing is that parameter passing is a bit different.

(1) test

Meaning: regular expressions are used to match strings. If the matching succeeds, true is returned. Otherwise, false is returned;
Syntax: re. test (string );
Let's start with the escape character:
/S space/S non-space/d Number/D Non-number/w characters (letters, numbers, underscores)/W non-characters
For example, judge whether a string is a number.

<! DOCTYPE> 

(2) search

Meaning: regular expressions are used to match strings. If a match is successful, the return value is-1. The return value is the same as the indexof () function in the string processing method.
Syntax: String. search (re );
[Color = Red] Note: The regular expression is case-sensitive by default. To make it case-insensitive, add id I. [/color]
In this example, a character in a case-insensitive deregularizedstring is matched.

<! DOCTYPE> 

(3) match

Meaning: regular expressions are used to match strings. If a match is successful, an array with a successful match is returned. Otherwise, Null is returned.
Syntax: String. match (re );
[Color = Red] Note: In regular expressions, the corresponding value is returned immediately after a successful match, and the matching will not continue. If you want to search for all, you need to add g (Global match) [/color]
Example: match consecutive numbers in a string and store them in an array (continuous numbers are used as an item of the array)

The "+" in the program appears at least once. Why?
We mentioned earlier that "the regular expression returns the corresponding value immediately after the matching is successful by default", and the result ends when a number is matched in the string, returns a number to an array. In this case, we need to use g to match each element.
Whether consecutive numbers are not determined or not, "+" can be used to satisfy the number of dynamic numbers.

<! DOCTYPE> 

(4) replace

Meaning: regular expressions are used to match strings. When a successfully matched string is replaced by a new string
Syntax: String. replace (re );
Example: replace all a in the string with B.

<! DOCTYPE> 

It will be written here for the time being to follow up with the new...

The above is all the content of this article. I hope you will like it.

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.