JS基础知识(下)(字符串,数字,算数,正则表达式)
总结:基本数据类型:string,number,null,boolean,undefined
复杂数据类型(引用数据类型):array,function,object,data
JavaScript 中的所有事物都是对象:字符串、数值、数组、函数...
字符串
- String 对象用于处理已有的字符块
- 获取字符串长度:length
var str = "hello world";alert(str);
- 索引:通过下标获取字符串指定位置的字符,但是不能改变该索引对应的值
var str = "hello world"
alert(str[0]);
str[0] = "H";//不会影响str,但也不会报错 字符串的几种方法:
- toUpperCase()将字符串全部转为大写,并返回原字符串不变
var str = "hello world";
alert(str.toUpperCase()); - toLowerCase()将字符串全部变小写,并返回原字符串不变
var str = "hello world";
alert(str.toLowerCase()); indexOf()
搜索指定字符串出现的位置,接收两个参数,第一个参数表示要查找的元素,第二个参数表示要查找的位置,若找到则返回对应元素所在的位置,否则返回-1
var s = 'hello world!';
alert(s.indexOf('world'));//返回6
alert(s.indexOf('World'));//返回-1
alert(s.indexOf('o'));//返回4
alert(s.indexOf('o',6));//从第六位开始查找,返回7concat()
将一个或多个字符串拼接起来,返回拼接到的新的字符串,原字符串不变
var str1 = "hello ";
var res = str1.concat(" world","!");
alert(res);//返回hello world!
alert(str1);//返回helloslice()
返回被操作字符的子字符串,原字符串不变,接受两个参数,字符串的起始位置和结束位置,返回的字符串不包含结束位置的字符,第一个参数要小于第二个参数,否则返回”“,若只有一个参数,返回起始位置到字符串结尾的所有字符串,若传递的参数为负数,将传入的负值与字符创的长度相加
var str = "hello world!";
alert(str.slice(3,7));//返回lo w
alert(str.slice(3));//返回lo world!
alert(str.slice(9,5));//返回""
alert(str.slice(-7,-3));//负数与长度相加,即str.slice(5,9)返回 wor
alert(str.slice(5,9));//返回 worsubstring()
当传入的参数是正数时,substring与slice的功能基本相同,唯一的区别是当第一个参数大于第二个参数时,方法将第二个参数作为截取的起始位置而将第一个参数作为截取结束的位置,且截取的字符串不包含第一个参数位置对应的值,当传入的参数是负值时,该方法会将所有的负值转化为0
var str = "hello world!";
alert(str.substring(3,7));//返回lo w
alert(str.substring(3));//返回lo world!
alert(str.substring(9,5));//返回 wor,即str.substring(5,9),不包含第九项
alert(str.substring(-7,-3));//负数与长度相加,即str.substring(0,0)返回""
alert(str.substring(-7,3));//负数与长度相加,即str.substring(0,3)返回helsubstr()
返回指定位置开始的指定长度的字符串,原字符串不变,若第二个参数缺省就一直截取到字符串结束,当传递的参数为负值时,方法会将负的第一个参数与字符串的长度相加,将负的第二个参数转化为0
var s = 'hello world!'
alert(s.substr(0, 5));//从索引0开始,截取5个字符串,返回hello
alert(s.substr(7)); //从索引7开始截取,一直到结束,返回orld!
alert(s.substr(-7,3));//负数与长度相加,即str.substr(5,3),返回 wo
alert(s.substr(-7,-3));//负数与长度相加,即str.substr(5,0),返回" "split()
基于指定的分隔符将一个字符串分割成多个字符串,并将结果存放在一个数组中,可以传两个参数,第一个参数为分隔符,第二个参数用于指定返回数组的大小,若省略该参数,则返回整个数组
var s = "1,23,45";
var arr1 = s.split(",");
alert(arr1);//返回数组["1","23","45"]
var arr2 = s.split(",",2);
alert(arr2);//返回数组["1","23"]trim()
删除元素前置及后缀的所有空格,然后返回结果,远数组不变
var str1 = " hello world ";
var str2 = str1.trim();
alert(str1);//返回" hello world "
alert(str2);//返回"hello world"
- toUpperCase()将字符串全部转为大写,并返回原字符串不变
数字
- JavaScript 数字可以使用也可以不使用小数点来书写
var pi=3.14; // 使用小数点
var x=34; // 不使用小数点 - 极大或极小的数字可通过科学(指数)计数法来写
var y=123e5; // 12300000
var z=123e-5; // 0.00123 - 所有 JavaScript 数字均为 64 位
- 精度:整数(不使用小数点或指数计数法)最多为 15 位。
小数的最大位数是 17,但是浮点运算并不总是 100% 准确(详情可看关于js浮点数计算精度不准确问题
)
算数(Math)
- 执行常见的算数任务
- 算数方法:
Math.round()
Math 对象的 round 方法对一个数进行四舍五入
document.write(Math.round(4.7))//5
Math.random()
返回一个介于 0 和 1 之间的随机数
document.write(Math.random()) //0.9370844220218102
Math.floor(Math.random())
返回一个介于 0 和 10 之间的随机数
document.write(Math.floor(Math.random()*11)) //3
- 算数值:
- 常数 Math.E
圆周率 Math.PI
2 的平方根 Math.SQRT2
1/2 的平方根 Math.SQRT1_2
2 的自然对数 Math.LN2
10 的自然对数 Math.LN10
以 2 为底的 e 的对数 Math.LOG2E
以 10 为底的 e 的对数 Math.LOG10E
- 常数 Math.E
正则表达式
- 正则表达式的缩写:RegExp
- 作用:存储检索模式,是一个描述字符模式的对象,ECMAScript (js)使用RegExp表示正则表达式
正则表达式专门针对字符串来操作的
- 定义正则表达式,一种使用new 运算符,二种使用字面量方式
- var reg = new RegExp(“hello”)
- var reg = /hello/ ig; (reg = /*/ pattern;)
进行验证正则表达式的方法:
test()、exec() 以及 compile()
- test()方法检索字符串中的指定值。返回值是 true 或 false
1.var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
//返回true
2.var reg = /[1-9][0-9]{4,14}/; //正则表达式以”/“开头,以”/“结尾,【】第一位【】第二位 {} 第二位这样的数字最少出现4次,最多出现14次
reg.test(qq)//测试qq这个字符串是否符合正则表达式,符合返回true 否则返回false
3.var reg = /hello/;
var str ="hello world";
alert(reg.test(str)); //true
reg.test(str);str写的是需要验证的字符串 作为参数传给test 将结果返回返回 布尔类型 只要是 含有 匹配的 返回true 否则为false
exec()方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
//返回 e
compile()方法用于改变 RegExp,既可以改变检索模式,也可以添加或删除第二个参数
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
patt1.compile("d"); //把e替换成 d
document.write(patt1.test("The best things in life are free"));
//返回true , false
- test()方法检索字符串中的指定值。返回值是 true 或 false