Common method (easy to back) summary

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_42601626/article/details/100589828

Array expansion method (hereinafter these methods do not change the original array) (check three times)

        1、indexOf(元素,start)  查找

            查找元素第一次在数组中出现的下标位置,如果没找到返回-1

            var arr=[1,2,3];

            alert(arr.indexOf(2,0));

        2、lastIndexOf(元素,start)

            查找元素在数组中从右往前找第一次出现的位置,如果没找到返回-1

        3、forEach(function(value,index,array){})  遍历数组

        4、map(function(value,index,array){

            return

        })      遍历数组,但是带有返回值,所以可以统一对返回值进行计算,比如统一加3

            var arr=[1,2,3];

            var arr1=arr.map(function(value,index,array){

                       return value+3;

            })

            alert(arr1);

        5、filter(function(value,index,array){

            return

        })      

            过滤数组,找出符合条件的元素,比如找出大于5的元素

            var arr=[2,6,9];

            var arr1=arr.filter(function(value,index,array){

                       return value>5;

                    })

                    alert(arr1);

Sring string search method for forwarding cut

        1、查

a.indexOf ( 'character string', start):

Find substring in the parent index of the first occurrence of the string, not found -1

b.lastIndexOf ( 'character string', start): Find substring from right to left the first position index occur in the parent string, not found -1

c.charAt (subscript): Find the character specified by index

                var strNew=new String('how do you do');

                alert(strNew.charAt(2));

d.charCodeAt (subscript): Find coded values ​​specified index position of the character

                var strNew=new String('how do you do');

                alert(strNew.charCodeAt(2));

        2、替

replace (old string new string) specified string with, a replacement can only

                var str="how do you do"

                alert(str.replace("do","de"));//how de you do

        3、截

subString (start, end) interception string specified range (can be taken forward from the back, it can not be negative)

Slice (start, end) intercepts range include a start character string excluding end (start and end values ​​may be negative, can not be taken from the rear forward)

substr (start, length) of the interception of a specified length

        4、转

toUpperCase () lowercase letters to uppercase

toLowerCase () uppercase letters lowercase

split ( 'cutting character', length): the string to the array

                var str="how do you do"

                alert(str.split('d',2));

Static method

        将编码转为字符

            String.fromcharCode()

            var str='how do you do'

            alert(String.fromCharCode(str.charCodeAt(2)));

        遍历所有的中文

            var str='';

            for(i=19968;i<=40869;i++){

                str+=String.fromCharCode(i)+' ';

            }

            document.write(str);    

Array-sectional CRUD complex row turn spell



            前增unshift()

                var arr=[2,4,6,8,10]

                console.log(arr.unshift(1,1,1));//打印出增加完元素之后的长度

                arr.unshift(0,0,0);在数组最前面中增加元素

                console.log(arr);//打印出增加元素后的数组

            后增push()

                var arr=[2,4,6,8]

                arr.push(10);

                console.log(arr);//2,4,6,8,10


            前删

                shift()

                var arr=[2,4,6,8]

                //console.log(arr.shift());//把前边的第一个元素弹出来

                arr.shift();

                console.log(arr);

            后删

                pop()把最后一个元素删除


            splice(起始下标,删除元素个数,增加元素内容)

                var arr=[2,4,6,8]

                arr.splice(1,2,3,5,7);

                console.log(arr);

                


            slice(start,end)截取指定位置的元素,包括起始位置,但不包括结束位置


            concat()

                var arr=[2,4,6,8]

                var arr1=[1,3,5,7]

                console.log(arr.concat(arr1));


            1.

                var arr=[2,4,6,8]

                var list=arr.slice();

                console.log(list);

            2.

                var arr=[2,4,6,8]

                var list=arr.concat();

                console.log(list);


	 reverse()    

            逆序,反转,并没有排序

                var arr=[2,4,6,8,3]

                console.log(arr.reverse())

            sort()

                var arr=[2,4,6,8,10]

                console.log(arr.sort());//10,2,4,6,8


            toString()

                var arr=[2,4,6,8]

                var str=arr.toString();

                console.log(typeof arr);//object

                console.log(typeof str);//string

            补充:进制转换

                var num=20

                var er=num.toString(2);

                console.log(er);//10100

            join()

                var arr=[2,4,6,8]

                var str=arr.join('啦')

                console.log(str);//2啦4啦6啦8

                console.log(typeof str);//string

js regular grammar

    元字符


                {}:限定{}前面的一个或一组字符连续出现的次数

                    {m}:m次

                    {m,}:至少m次

                    {m,n}:m-n次

                    

                []:限定范围

                    [a-z]:匹配a-z任意一个字符

                    [A-Z]:匹配A-Z任意一个字符

                    [a-zA-Z]:匹配26个英文字母(大小写)

                    [0-9]:匹配一个数字

                    [0-9a-zA-Z]:匹配一个字符可以是数字、字母和下划线

                    [\u4e00-\u9fa5]:匹配任意一个中文字符

                    [^0-9]:匹配非数字

                    [^a-zA-Z]:匹配非字母

                        ^表示取反

                ():表示组

                


                *

                    重复0-无限次

                +

                    重复1-无限次


                    重复0-1次


                ^

                    定头:1.用在正则表达式的开头部分,表示限定字符串的开头必须是指定的字符串

                        2.用在[]的开头,表示取反

                $

                    定尾:用在正则表达式的结尾部分,表示限定字符串的结尾必须是指定的字符

                    

            三个一

                .   表示可以匹配任意一个字符

                |   表示或者,一般和组一起用

                        判断性别        /^(男|女)$/

                        判断格式是否正确

                \   转义符

                        \d 匹配数字,相当于[0-9]

                        \D 匹配非数字,相当于[^0-9]

                        \w 匹配数字、字母、下划线

                        \W 匹配非数字、字母、下划线

                        \s 匹配空白字符串

                        \S 匹配非空白字符串

                        \b 匹配单词边界

                        \B匹配非单词边界

Guess you like

Origin blog.csdn.net/qq_42601626/article/details/100589828