# Getting started with JavaScript regular expression basics

Source: Internet
Author: User

I haven't seen regular expressions for a long time, happened to use today, restudying A, here record down, share to everyone, are some basic knowledge, focus on the regular expression of the 4 commonly used methods, 50% of extrapolate practice in the original.

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:

?

 1 2 3 4 5 6 7 8 9 10 11 var str= ' dgh6a567sdo23ujaloo932 '; function GetNumber (obj) {var arr=[]; for (var i = 0; i < obj.length i++) {if (Obj.charat (i) >= ' 0 ' &&obj.char at (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:

?

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 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 concatenate adjacent digits} else{//whenever the number of connections is disconnected, this executes if (temp) {Arr.push (temp); Temp= '; } }; The function of if (temp) {//Here is to display the final number, not to explain Arr.push (temp); temp= ';} return arr; };

So we use regular expressions to solve this function:

?

 1 2 3 4 5 6 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:

?

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the

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

?

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

(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: In the regular is the default is case-sensitive, if you want to make it case-insensitive is to add the ID i; [/color]

example, case-insensitive to match a character in a string

?

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

(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.

?

 1, 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

(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

?

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

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

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

Related Keywords:
Related Article

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD