js中exec、test、match、search、replace、split用法
2018-10-12 13:50
741 查看
exec:对string进行正则处理,并返回匹配结果.array[0]为原字符串,array[i]为匹配在整个被搜索字符串中的位置。
test:测试string是否包含有匹配结果,包含返回true,不包含返回false。
match(pattern) :根据pattern进行正则匹配,如果匹配到,返回匹配结果,如匹配不到返回null
search(pattern) :根据pattern进行正则匹配,如果匹配到一个结果,则返回它的索引数;否则返回-1
replace(pattern,replacement) :根据pattern进行正则匹配,把匹配结果替换为replacement
split(pattern) :根据pattern进行正则分割,返回一个分割的数组
实例:
replace
最简单的算是能力就是简单的字符替换。示例代码如下:
split
输出结果为:
How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you
test:测试string是否包含有匹配结果,包含返回true,不包含返回false。
match(pattern) :根据pattern进行正则匹配,如果匹配到,返回匹配结果,如匹配不到返回null
search(pattern) :根据pattern进行正则匹配,如果匹配到一个结果,则返回它的索引数;否则返回-1
replace(pattern,replacement) :根据pattern进行正则匹配,把匹配结果替换为replacement
split(pattern) :根据pattern进行正则分割,返回一个分割的数组
实例:
function checkForm() {
var u = document.form_name.check.value;
var s = /^[-a-zA-Z0-9_]+(\.[-a-zA-Z0-9_]+)*@[-a-zA-Z0-9_]+(\.[-a-zA-Z0-9_]+)*$/;
var a = s.exec(u);
var a = s.test(u);
var a = u.match(s);
var a = u.search(s);
alert(a);
}
replace
最简单的算是能力就是简单的字符替换。示例代码如下:
var u = "javascript is a good script language";
//在此我想将字母a替换成字母A
var s = /a/g;
var a = u.replace(s,"A");
</script>
split
<script type="text/javascript">
var str="How are you doing today?"
document.write(str.split(" ") + "<br />")
document.write(str.split("") + "<br />")
document.write(str.split(" ",3))
</script>
输出结果为:
How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you
您可能感兴趣的文章:
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js正则表达式之match函数讲解
- JS正则中的match与exec使用说明
- javascript中match函数的用法小结
- 简介JavaScript中charAt()方法的使用
- js charAt的使用示例
- JavaScript charCodeAt方法入门实例(用于取得指定位置字符的Unicode编码)
- js Map List 遍历使用示例
- JS Map 和 List 的简单实现代码
- js实现的map方法示例代码
- js正则表达式之search方法讲解
- JS常见疑难点分析之match,charAt,charCodeAt,map,search用法分析
相关文章推荐
- js中exec、test、match、search、replace、split用法
- js中exec、test、match、search、replace、split用法
- js中exec、test、match、search、replace、split、indesOf()用法
- js正則函數 match、exec、test、search、replace、split 使用介紹集合
- javascript正则表达式用法 (test|match|search|replace|split|exec)
- js正则函数match、exec、test、search、replace、split使用介绍集合
- JavaScript正则表达式小结(test|match|search|replace|split|exec)
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js正则函数match、exec、test、search、replace、split使用介绍
- js正则函数match、exec、test、search、replace、split使用介绍
- js正则函数match、exec、test、search、replace、split使用介绍集合
- javascript正则表达式(test|match|search|replace|split|exec)
- Nodejs正则表达式函数之match、test、exec、search、split、replace
- js中exec,test,match,search,split等方法的使用
- Nodejs正则表达式函数之match、test、exec、search、split、replace使用详解
- js正则函数match、exec、test、search、replace、split使用介绍集合
- Nodejs正则表达式函数之match、test、exec、search、split、replace使用详解
- test(),compile(),exec(),match(),search(),replace(),split()
- js 中exec、test、match、search、replace、split用法
- js正则函数match、exec、test、search、replace、split使用介绍集合