JavaScript Regular Expression Basics introductory _javascript Tips

Source: Internet
Author: User

Where is the benefit of regular expressions, let's take a look at the following:
We use the method of processing strings in JS to write out the function of the number 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"]

The above method we take out the numbers in the string, but we are not satisfied, we need [' 6 ', ' 567 ', ' 23 ', ' 932 '] form to transform the function:

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);//now connects adjacent digits
        }
        else{//whenever the number of connections is disconnected, this executes
          if (temp) {
            Arr.push (temp);
            Temp= ';
          }}
        ;
    }
    if (temp) {//The function here is to display the last digit, reason does not want to explain
            arr.push (temp);
            Temp= ';
          }
    return arr;
  };

So we use regular expressions to solve this function:

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

Take a complete look at the results of the program running:

<! doctype>  

As we can see from the example above, the regular expression method has the same effect, but the code is shorter and more efficient, which is the positive benefit.
Is the result of more efficient processing of strings, just as the string processing method is more efficient and concise (only strings can be processed)

Here we have a systematic study, regular several common methods:

Before you say regular writing, and just as with other object array (), object (), Date (), and so on, there is a way to initialize
var re=/here to write a matching thing, do not write words is to gaze at the symbol/; This is the simple creation of regular objects, I will use it to replace the following articles directly
var re=new RegExp (); This is also possible to create a way, you know, just and shorthand is different from parameter transfer

(1) test

Meaning: Regular to match the string, when the match returns true successfully, otherwise, return false;
Syntax: Re.test (String);
Let's say some escape characters first:
/s non-space/d/numeric/non-numeric/w characters (letters, numbers, underscores)/w non-characters
For example: To determine whether a string is a number

<! doctype>
 
 

(2) Search

Meaning: Regular to match the string, when the match successfully returned to match the success of the position, conversely, return 1; and the indexof () function in the string processing method
Syntax: string. Search (re);
[color=red] Note: The default in the regular is case-sensitive, to make it case-insensitive is to add the identification I;[/color]
example, case-insensitive to match a character in a string

<! doctype>
 
 

(3) match

Meaning: The regular goes to match the string, when the match successfully returns the matching successful array, conversely, returns null
Syntax: string. Match (re);
[color=red] Note: The default in the regular is to return the corresponding value as soon as the match succeeds, and will not continue to match. To find all you need to add g (Global match) [/color]
Example: matching consecutive digits in a string and depositing it in an array (consecutive numbers as an array)

The "+" in the program is a match that appears at least once, why do you do this?
We mentioned earlier that "the default is to return the corresponding value as soon as the match succeeds," then the string matches to a number and ends, and a number is returned to the array, which is what we need to match each element with G.
Do you find the number of consecutive numbers not determined, with "+" can satisfy the number of dynamic numbers.

<! doctype>
 
 

(4) Replace

Meaning: Regular to match the string, when the matching successful string is replaced by the new string
Syntax: string. replace (re);
Example: Replace all a in a string with B

<! doctype>
 
 

Temporarily write here to follow up with the new ...

The above mentioned is the entire content of this article, I hope you can enjoy.

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.