JavaScript common methods Summarize _javascript skills

Source: Internet
Author: User
1.replace ()
Cases:
<script type= "Text/javascript" >
var str= "This is text, there is a text below, this text and that text is not the same";
document.write (Str.replace (/text/g, "test"))//This is to convert all text to test
var str1= "This is text, there is a text below, this text and that text is not the same";
document.write (Str1.replace (/text/gi, "Test"));//case-insensitive to convert all text to test
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"))//case-insensitive to convert text to test
</script>
2.search ()
Cases:
<script type= "Text/javascript" >
var str= "Zhangqingyu";
document.write (Str.search (/a/));----->2, if there is no string you want to find, return-1
var str1= "Zhangqingyu";
document.write (Str1.search (/a/));---->-1, there is no
document.write (Str1.search (/a/i));---->2 This method is a fuzzy query
</script>
3.slice () Select a part of string from a string to generate a new string
Cases:
<script type= "text/javascript>
var str= "Zhangqingyu";
document.write (Str.slice (0,2));---->zh
document.write (Str.slice ( -3,11));--->gyu, if the first argument is negative, start with the string length + first argument
document.write (Str.slice (0,-4));---->zhangqi, if the second argument is negative, the end data is string length + second argument
document.write (Str.slice (4));------>gqingyu, if there is only one argument, take it from the argument to the end of the string
</script>
4.match () finds the specified value from a string that returns the specified value, not the position of the specified value, unlike search
Cases:
<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 () displays a string as a link
Cases:
<script type= "text/javascript>
var url= "click here";
document.write (Url.link ("http://www.hao123.com"));
</script>
6.lastIndexOf () Gets the position of the value to be queried in this string and does not exist returns-1
Cases:
<script type= "text/javascript>
var str= "Zhangqingyu";
document.write (Str.lastindexof ("G"));---->8
document.write (Str.lastindexof ("G", "1"));
</script>

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.