test:测试string是否包含有匹配结果,包含返回true,不包含返回false。
代码语言:javascript复制<script type="text/javascript">
var str = "bbs.byr.cn";
var reg = /b/;
var ret = reg.test(str);
alert(ret); //true
</script>
match:根据pattern进行正则匹配,如果匹配到,返回匹配结果,如匹配不到返回null
代码语言:javascript复制<script type="text/javascript">
var str = "ThIS isn't me";
var reg = /is/ig;
var ret = str.match(reg);
alert(ret); //IS, is
console.log(ret); //["IS", "is"]
</script>
search :根据pattern进行正则匹配,如果匹配到一个结果,则返回它的索引数;否则返回-1
代码语言:javascript复制<script type="text/javascript">
var str = "This isn't me";
var reg = /is/;
var ret = str.search(reg);
alert(ret);//2
</script>
replace:根据pattern进行正则匹配,把匹配结果替换为replacement
代码语言:javascript复制<script type="text/javascript">
var str = "i love china!";
var pattern = /i/g;
var ret = str.replace(pattern, "I");
alert(ret); //I love chIna!
</script>
split:根据pattern进行正则分割,返回一个分割的数组
代码语言:javascript复制<script type="text/javascript">
var str = 'http://www.baidu.com/';
var reg = /W/;
var ret = str.split(reg);
console.log(ret); //["http", "", "", "www", "baidu", "com", ""]
</script>
exec:对string进行正则处理,并返回匹配结果.array0为原字符串,arrayi为匹配在整个被搜索字符串中的位置。
代码语言:javascript复制<script type="text/javascript">
var str = "i love china!";
var reg = /iB/g;
var ret = reg.exec(str);
alert(ret); // i
console.log(ret); //["i", index: 9, input: "i love china!"]
</script>
本文采用 「CC BY-NC-SA 4.0」创作共享协议,转载请标注以下信息:
原文出处:Yiiven https://cloud.tencent.com/developer/article/2193082