The return value of the match,test,exec,search in a regular expression

Source: Internet
Author: User

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

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.