您的位置:首页 > Web前端 > JavaScript

js常用的正则表达操作

2017-01-09 16:01 232 查看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
var past=/helloword/gi;
var str='helloword my HelloWord baby';
console.log(past.test(str));//返回true or false

console.log(past.exec(/aa/));//返回匹配到的字符串及其位置 匹配不到返回null
console.log(past.exec(str));//返回匹配到的字符串及其位置 匹配不到返回null
console.log(str.match(past));//返回匹配到的字符串        匹配不到返回null
console.log(str.match(/aa/));//返回匹配到的字符串        匹配不到返回null
console.log(str.search(/1/));//返回匹配到的字符串位置     匹配不到返回-1
console.log(str.replace(past,"你好"));//字符串替换
console.log(str.split(' '));//字符串拆分成数组
console.log(str.split(/\s+/));//s+匹配空字符串
</script>
</head>
<body>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: