Today, I was asked a regular expression, because for a long time don't suddenly don't know how to use, only know that there is such a thing. Then went to the Internet to check, feel write a lot, but the explanation of a little vague, today I come to talk about.
The use of 1,match
A, in the case of no Global "G":
var str= "wo Shi Zhong guo ren";
Console.log (Str.match (/o/));
The results returned are: ["O", index:1, Input: "Wo shi Zhong guo ren"];
A, in the case of adding the global "G":
var str= "wo Shi Zhong guo ren";
Console.log (Str.match (/o/g));
The results returned are: ["O", "O", "O"];
The use of 2,exec
var str= "wo Shi Zhong guo ren";
var re=/o/;
Console.log (Re.exec (str));
The results returned are: ["O", index:1, Input: "Wo shi Zhong guo ren"];
Here I think you can see that exec returned is not the same as match does not add global "G"? You got the right answer.
The use of 3,test
var str= "wo Shi Zhong guo ren";
var re=/o/;
Console.log (Re.test (str));
The result returned is: true (False if the return is not found);
The use of 4,search
The use of search is very simple, it is the same as indexof, is to find index, and from the back to the number.
var str= "wo Shi Zhong guo ren";
var re=/o/;
Console.log (Str.search (re));
The returned result is: 1;
The return value of the match,test,exec,search in a regular expression