var reg=/hello/ 或者 var reg=new RegExp("hello")
"abchello".search(/hello/); // 3
"abchello".replace(/hello/,"hi"); // "abchi"
"abchelloasdasdhelloasd".split(/hello/); //["abc", "asdasd", "asd"]
"abchelloasdasdhelloasd".match(/hello/); //["hello"] "abchelloasdasdhelloasd".match(/hello/g); //["hello","hello"]
/hello/.test("abchello"); // true
var reg=/hello/g;
reg.exec("abchelloasdasdhelloasd"); // ["hello"]
var reg=/hello/g;
reg.lastIndex; //0
reg.exec("abchelloasdasdhelloasd"); // ["hello"]
reg.lastIndex; //8
reg.exec("abchelloasdasdhelloasd"); // ["hello"]
reg.lastIndex; //19
reg.exec("abchelloasdasdhelloasd"); // null
reg.lastIndex; //0
//匹配数字: \d "ad3ad2ad".match(/\d/g); // ["3", "2"] //匹配除换行符以外的任意字符: . "a\nb\rc".match(/./g); // ["a", "b", "c"] //匹配字母或数字或下划线 : \w "a5_ 汉字@!-=".match(/\w/g); // ["a", "5", "_"] //匹配空白符:\s "\n \r".match(/\s/g); //[" ", " ", ""] 第一个结果是\n,最后一个结果是\r //匹配【单词开始或结束】的位置 : \b "how are you".match(/\b\w/g); //["h", "a", "y"] // 匹配【字符串开始和结束】的位置: 开始 ^ 结束 $ "how are you".match(/^\w/g); // ["h"]
// 匹配字母 a-z 之间所有字母
/[a-z]/
// 匹配Unicode中 数字 0 到 字母 z 之间的所有字符
/[0-z]/
// unicode编码查询地址:
//https://en.wikibooks.org/wiki/Unicode/Character_reference/0000-0FFF
//根据上面的内容,我们可以找出汉字的Unicode编码范围是 \u4E00 到 \u9FA5,所以我们可以写一个正则表达式来判断一个字符串中是否有汉字
/[\u4E00-\u9FA5]/.test("测试"); // true
//重复n次 {n}
"test12".match(/test\d{3}/); // null
"test123".match(/test\d{3}/); // ["test123"]
//重复n次或更多次 {n,}
"test123".match(/test\d{3,}/); // ["test123"]
//重复n到m次
"test12".match(/test\d{3,5}/); // null
"test12345".match(/test\d{3,5}/); // ["test12345"]
"test12345678".match(/test\d{3,5}/); // ["test12345"]
// 匹配字符test后边跟着数字,数字重复0次或多次
"test".match(/test\d*/); // ["test"]
"test123".match(/test\d*/); // ["test123"]
//重复一次或多次
"test".match(/test\d+/) ; // null
"test1".match(/test\d*/); //["test1"]
//重复一次或0次
"test".match(/test\d?/) ; // null
"test1".match(/test\d?/); //["test1"]
// 数字重复3~5次,满足条件的情况下返回尽可能少的数字
"test12345".match(/test\d{3,5}?/); //["test123"]
// 数字重复1次或更多,满足条件的情况下只返回一个数字
"test12345".match(/test\d+?/); // ["test1"]
"abchellodefhellog".match(/h(ell)o/g); //["hello", "hello"]
"abchellodefhellog".match(/h(ell)o/); //["hello", "ell"] // 我们可以用match函数来分解url,获取协议、host、path、查询字符串等信息 "http://www.baidu.com/test?t=5".match(/^((\w+):\/\/([\w\.]+))\/([^?]+)\?(\S+)$/); // ["http://www.baidu.com/test?t=5", "http://www.baidu.com", "http", "www.baidu.com", "test", "t=5"]
/h(ell)o/g.exec("abchellodefhellog"); //["hello", "ell"]
"asdasd hi asdad hello asdasd".replace(/hi|hello/,"nihao"); //"asdasd nihao asdad hello asdasd" "asdasd hi asdad hello asdasd".split(/hi|hello/); //["asdasd ", " asdad ", " asdasd"]
"abc acd bbc bcd ".match(/(a|b)bc/g); //["abc", "bbc"]
// 匹配重复的单词
/(\b[a-zA-Z]+\b)\s+\1/.exec(" asd sf hello hello asd"); //["hello hello", "hello"]
/(hello)\sworld/.exec("asdadasd hello world asdasd") // ["hello world", "hello"],正常捕获结果字符串和分组字符串
/(?:hello)\sworld/.exec("asdadasd hello world asdasd") // ["hello world"]
"/static/app1/js/index.js".replace(/(\/\w+)\.js/,"$1-v0.0.1.js"); //"/static/app1/js/index-v0.0.1.js"
"/static/app1/js/index.js".replace(/(?:\/\w+)\.js/,"$1-v0.0.1.js"); //"/static/app1/js$1-v0.0.1.js"
/hello\s(?=world)/.exec("asdadasd hello world asdasd") // ["hello "]
/hello\s(?!world)/.exec("asdadasd hello world asdasd") //null
// 全局匹配 g "abchelloasdasdhelloasd".match(/hello/); //["hello"] "abchelloasdasdhelloasd".match(/hello/g); //["hello","hello"] //忽略大小写 i "abchelloasdasdHelloasd".match(/hello/g); //["hello"] "abchelloasdasdHelloasd".match(/hello/gi); //["hello","Hello"]
"aadasd\nbasdc".match(/^[a-z]+$/g); //null 字符串^和$之间有换行符,匹配不上 [a-z]+ ,故返回null "aadasd\nbasdc".match(/^[a-z]+$/gm); // ["aadasd", "basdc"] ,改变^$的含义,让其匹配一行的开头和末尾,可以得到两行的结果
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有