web.js.字符串与正则表达式操作
1.substring
varstr='abcdef'; alert(str.substring(2,5));//cde不包括结束位置 alert(str.substring(1));//bcdef1
2.split
varstr='a*b*cd*ef';
alert(str.split('*'));//分割字符1
3.search
varstr='acef'; alert(str.search(‘a'));//0查找字符位置 alert(str.search(‘f'));//3 alert(str.search(‘ce'));//1 alert(str.search(‘o'));//-1匹配失败则-1
正则
varre=newRegExp('b','i');//i不考虑大小写
//或者varre=/b/i;
varstr='abcdef';//将b换成B同样的结果,如果去掉i就不行了
alert(str.search(re));
1.match
varstr='asdf34656cvs33'; varre=/\d/g; alert(str.match(re));//3,4,6,5,6,3,3match获取匹配的项目1 varstr='asdf34656cvs33'; varre=/\d+/g;//全局匹配:g——global,+表示一次或者多次 alert(str.match(re));//34,656,33
2.replace
varstr='asdf34656cvs33'; varre=/\d+/g; varre2=/\d/g; alert(str.replace(re,'*'));//asdf**cvs*; alert(str.replace(re2,'*'));//asdf*****cvs**1
去掉敏感词
varstr='河南一村民开封哈哈' varre=/河南|开封/g;//去掉敏感词河南或开封 varre1=/河南|开封/; alert(str.replace(re,'*')); alert(str.replace(re1,'*'))//没有去掉开封,自己试试结果1
3.[]任意字符,范围
[abc]
例子:o[usb]t——obt、ost、out
[a-z]、[0-9]
例子:id[0-9]——id0、id5
[^a](排除a外的一切)
例子:o[^0-9]t——oat、o?t、ot
组合
[a-z0-9A-Z]
以上所述是小编给大家介绍的web.js.字符串与正则表达式操作,希望对大家有所帮助,如果大家有任何疑问欢迎各我留言,小编会及时回复大家的!