This article mainly introduces relevant information about web.js. string and regular expression operations. Friends in need can refer to
1.substring
var str='abcdef'; alert(str.substring(2, 5)); //cde不包括结束位置 alert(str.substring(1));//bcdef1
2.split
var str='a*b*cd*ef'; alert(str.split('*'));//分割字符1
3.search
var str='acef'; alert(str.search(‘a'));//0查找字符位置 alert(str.search(‘f'));//3 alert(str.search(‘ce'));//1 alert(str.search(‘o'));//-1匹配失败则-1
regular
var re=new RegExp('b', 'i'); //i不考虑大小写 //或者var re=/b/i; var str='abcdef';//将b换成B同样的结果,如果去掉i就不行了 alert(str.search(re));
1.match
var str='asdf 34 656 cvs33'; var re=/\d/g; alert(str.match(re));//3,4,6,5,6,3,3match 获取匹配的项目1 var str='asdf 34 656 cvs33'; var re=/\d+/g;//全局匹配:g——global,+表示一次或者多次 alert(str.match(re));//34,656,33
2.replace
var str='asdf 34 656 cvs33'; var re=/\d+/g; var re2=/\d/g; alert(str.replace(re,'*'));//asdf * * cvs*; alert(str.replace(re2,'*'));//asdf ** *** cvs**1
Remove sensitive words
var str='河南 一村民 开封 哈哈' var re=/河南|开封/g;//去掉敏感词河南或开封 var re1=/河南|开封/; alert(str.replace(re,'*')); alert(str.replace(re1,'*'))//没有去掉开封,自己试试结果1
3 .[] Any character, range
[abc]
Example: o[usb]t——obt, ost, out
[a-z ], [0-9]
Example: id[0-9]——id0, id5
[^a](exclude everything except a)
Example: o[^0-9]t——oat, o?t, o t
combination
[a-z0-9A-Z]
【Related Recommendations】
1. Special Recommendation:"php Programmer Toolbox" V0.1 version download
2. Free js online video tutorial
##3.php.cn Dugu Jiujian (3) - JavaScript video tutorial
The above is the detailed content of web.js.String and regular expression operation example tutorial. For more information, please follow other related articles on the PHP Chinese website!