Summary of common javascript methods

Source: Internet
Author: User

1. replace ()
Example:
<Script type = "text/javascript">
Var str = "this is text, and there is a text below, which is different from that text ";
Document. write (str. replace (/text/g, "test"); // converts all texts to test
Var str1 = "this is Text, and there is a Text below, which is different from that Text ";
Document. write (str1.replace (/text/gi, "test"); // converts all texts to test in case insensitive.
Var str2 = "this is text ";
Document. write (str2.replace (/text/, "test"); // convert text to test
Var str3 = "this is Text ";
Document. write (str3.replace (/text/I, "test"); // converts Text to test without case sensitivity.
</Script>
2. search ()
Example:
<Script type = "text/javascript">
Var str = "zhangqingyu ";
Document. write (str. search (/a/); -----> 2. If the string you want to search for does not exist,-1 is returned.
Var str1 = "zhAngqingyu ";
Document. write (str1.search (/a/); ---->-1, it does not exist
Document. write (str1.search (/a/I); ----> 2 this method is fuzzy query.
</Script>
3. slice () selects a part of the string to generate a new string
Example:
<Script type = "text/javascript>
Var str = "zhangqingyu ";
Document. write (str. slice (0, 2); ----> zh
Document. write (str. slice (-); ---> gyu. If the first parameter is negative, it starts to be a String Length + the first parameter
Document. write (str. slice (0,-4); ----> zhangqi, if the second parameter is negative, the end data is the string length + the second parameter
Document. write (str. slice (4); ------> gqingyu. If there is only one parameter, It is retrieved from the parameter until the end of the string.
</Script>
4. match () searches for the specified value from the string. It returns the specified value instead of the position of the specified value. It is different from search.
Example:
<Script type = "text/javascript>
Var str = "hello word! ";
Document. write (str. match ("word"); ---> word
Document. write (str. match ("Word"); ----> null
Document. write (str. match ("word! "); ----> Word!
</Scrip>
5. link () shows a string as a link.
Example:
<Script type = "text/javascript>
Var url = "Click here ";
Document. write (url. link ("http://www.hao123.com "));
</Script>
6. lastIndexOf () to obtain the position of the value to be queried in this string. If no value exists,-1 is returned.
Example:
<Script type = "text/javascript>
Var str = "zhangqingyu ";
Document. write (str. lastIndexOf ("g"); ----> 8
Document. write (str. lastIndexOf ("g", "1 "));
</Script>

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.