var str = new String();
var str = new String('hello world');//初始化str,str.length = 11;
var strValue = new String('hello world');
console.log(strValue.charAt(1));//e
console.log(strValue[1]);//e,IE7及以下版本使用这种方式,会返回undefined
var strValue = new String('hello world');
console.log(strValue.charCodeAt(1));//101
var str = 'hello world'
str.indexOf('l');//2,返回找到的第一个字符的位置
str.indexOf('l',6);//9
var str = new String('hello');
var result = str.concat(' world');
console.log(result);//hello world
typeof result//"string"
var str = 'hello'; str.slice(0,2);//"he",第一个参数指定字符串开始的位置,第二个参数表示字符串到哪里结束 str.slice(-3);//"llo",o代表-1,依次倒数,-3代表倒数第三个的l str.slice(-2,-1);//"l",同理,-2代表倒数第二个l,-1代表倒数第一的o
var str = 'hello'; str.substring(0,2);//"he",此时的参数意义同str.slice(0,2) str.substring(-3);//"hello",substring()方法会把所有负值参数转换为0 str.substring(-3,-2);//"",同上
var str = 'hello'; str.substr(1,2);//"el",第一个参数指定字符串的开始位置,第二个参数指定的则是返回的字符个数 str.substr(-3);//"llo",此时的参数意义同str.slice(-3) str.substr(-3,-1);//"",substr()方法会将负的第二个参数转换为0
var color = 'blue,red,orange';
color.split();//["red,blue,orange"],长度为1
color.split(',');//["blue", "red", "orange"],长度为3
var color = 'blue-red-orange';
color.split('-');//["blue", "red", "orange"],长度为3
color.split(',',2);//["blue", "red"]
var str = 'hello'; str.toUpperCase();//"HELLO" str.toLowerCase();//"hello"
var str = ' hello world '; str.trim()//"hello world"
var text = 'cat, bat, sat, fat'; var pattern = /.at/; var matches = text.match(pattern); matches // ["cat"] matches.input // "cat, bat, sat, fat" matches.index // 0
var text = 'cat, bat, sat, fat'; var pattern = /at/; text.search(pattern) // 1
| 字符序列 | 替换文本 |
|---|---|
| $$ | $ |
| $& | 匹配整个pattern中的子字符串 |
| $' | 匹配的子字符串之后的子字符串 |
| $` | 匹配的字符串之前的子字符串 |
| $n | 匹配第n个捕获组中的子字符串,$1 |
| $nn | 匹配第nn个捕获组的子字符串,$01 |
var text = 'xxx-love-xxx';
var pattern = /xxx/g;
var result = text.replace(pattern,'2')
result// "2-love-2"
text.replace(/(xxx)-\w{4}-(xxx)/g,'I love YOU');//"I love YOU"
var text = 'xxx-love-xxx';
var pattern1 = /xxx/g;
var result = text.replace(pattern1,'$$')
result// "$-love-$"
var result = text.replace(pattern1,'$&2')
result//"xxx2-love-xxx2"
var result = text.replace(pattern1,'$\'')
result//"-love-xxx-love-"
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有