<!DOCTYPE html>
<html>
<head>
<title>正则表达式</title>
</head>
<body>
</body>
<script>
var s="hellow wordle gf,i love u!";
var ss=/gf/;
console.log(ss.test(s));/*字符串匹配,返回值布尔类型,一旦匹配上,不会向后进行匹配*/
console.log(ss.exec(s));/*字符串匹配,返回值null或数组类型*/
/*g全局匹配,i不区分大小写,+以空格隔开,没有+以单个字符分开*/
var s="hellow wordle gf,i love gf!";
console.log(s.match(/gf/gi));/*字符串匹配,返回值null或数组类型,加g可匹配两个gf,不加g只找到第一个gf*/
/*字符串正则*/
var s="hellow wordle gf,i love gf!";
console.log(s.search(/Gf/i));/*字符串匹配,返回值-1或int索引*/
var s="hellow hugger,oh i am hugger";
console.log(s.replace(/hugger/,'ge'));/*替换字符串,返回值string*/
console.log(s.replace(/hugger/g,'ge'));
var s="hellow hugger oh i am hugger";/*分割字符串转数组*/
console.log(s.split(" "));
/*————————————————————正则匹配————————————————————————————*/
var s="is this all there is,987 A";
console.log(s.match(/[a-h]/g));/*全局匹配a-h的任意字符,返回值数组*/
console.log(s.match(/[abc]/g));/*全局匹配abc的任意字符,返回值数组*/
console.log('---')
console.log(s.match(/[^a-z]/g));/*全局匹配不在a-h的任意字符,返回值数组*/
console.log(s.match(/[0-9]/g));/*全局匹配不在0-9的任意字符,返回值数组*/
console.log(s.match(/[a-z]/g));/*全局匹配不在a-z的任意字符,返回值数组*/
console.log(s.match(/[A-Z]/g));/*全局匹配不在A-Z的任意字符,返回值数组*/
console.log(s.match(/[A-z]/g));/*全局匹配不在A-z的任意字符,返回值数组*/
console.log(s.match(/this|all/g));/*全局查找this或者all的字符串*/
console.log(s.match(/9.7/g));/*全局查找9开头,7结束,中间任意一个字符的字符串,除了换行和结束符*/
var s="cive 100% g_ !你好";
console.log(s.match(/\w/g));/*查找单词字符,字母,数字,下划线*/
var s="end cive 100% g_ !你好,1 2 end";
console.log(s.match(/\W/g));/*查找非单词字符*/
console.log(s.match(/\d/g));/*全局查找数字,没有加号,会将100拆开成1,0,0三个字符串*/
console.log(s.match(/\d+/g));/*全局查找数字,有加好,100是一个字符串*/
console.log(s.match(/\D/g));/*全局查找非数字*/
console.log(s.match(/\s/g));/*查找空白字符。空格,tab,换行,回车*/
console.log(s.match(/\S/g));/*查找非空白字符。*/
console.log(s.match(/\ben/g));/*匹配单词边界是否包含en字符串*/
console.log(s.match(/en\b/g));/*匹配单词边界,即以en结束或者开头*/
console.log(s.match(/end\B/g));/*匹配非单词边界*/
var s="ayghuyyh gyyyh \n kl";
console.log(s.match(/\n/g));/*查找换行字符*/
console.log(s.match(/y+/g));/*字符串否含一个y,或多个连续y的字符串*/
console.log(s.match(/a*/g));/*包含0个或者多个a*/
console.log(s.match(/a?/g));/*包含0个或者1个a*/
console.log(s.match(/y{2}/g));/*连续包含2个yy*/
console.log(s.match(/y{2,3}/g));/*连续包含2个yy或者3个yy*/
console.log(s.match(/y{2,}/g));/*连续至少包含2个yy*/
console.log(s.match(/kl$/g));/*以kl结尾*/
console.log(s.match(/^a/g));/*以a开头*/
/******常见正则
http://www.w3cfuns.com/tools.php?mod=regex
*******/
</script>
</html>
正则表达式
最新推荐文章于 2024-05-20 10:35:51 发布