正则表达式简单操作

使用单个字符串来描述、匹配一系列符合某个语法规则的字符串,它通常被用来检索、替换那些符合某个模式的文本。比如,登录框,用户输入信息,用它判断输入信息是不是符合格式。JS里有个对象,通过它创建一个规则,对字符串进行匹配,看看字符串是不是匹配规则,匹配上了就是true,否则false。

创建

内置对象RegExp:

var reg=new RegExp('<%[^%>]+%>','g')   //构造函数太复杂了

字面量写法:

var reg=/<%[^%>]%>/g
undefined
reg
/<%[^%>]%>/g
有几个修饰符:
//最后的g代表global,全局搜索,不添加只搜到第一个结果停止。
//i:ingore case,忽略大小写,默认大小写敏感
//m:multiple lines,多行搜索
var reg=/hello/gi
undefined
reg
/hello/gi               //在控制台展示效果像是字符串形式,其实reg是个复杂对象,展示出来看起来像是字符串。

最根本的是函数写法,符合原意,但是太麻烦,字面量写法就是用符号代替函数的意义,应用上更方便了。构造函数里面格式是字符串,字面量写法里面是正常的写法。

元字符

在正则表达式中有特殊意义的字符,可以规定其前导字符,并不多。

([{\^$|)?*+.

如果想用这些元字符的本来意义,要先转义,用\。

\t         //水平制表符
\r            //回车符
\n         // 换行符
\f         //换页符
\cX         //与X对应的控制字符
\v          //垂直制表符
\0             //空字符

字符类

var reg=/ab\t/

正则表达式只是匹配字符串,作用就太小了,它能匹配类,某一类有共同规则的。

范围

var reg=/[a-z]/ig
var reg=/[0-9]/ig
var reg=/[abc012]/ig
//[]出现,代表一个字符的挑选,只能代表一个字符的占位哦。

取反

var reg=/[^abc012]/ig
//只要不是abc012它们中的任何一个,就会被选中。

区间

[a-z]
var reg=/[0-9a-zA-Z]/ig

一定意义的匹配

. === [^\r\n]除回车换行外的所有字符
\d === [0-9]数字字符
\D === [^0-9]非数字字符
\s === [\t\n\x0B\f\r]空白符:回车换行制表空格就可以了,\x0B是垂直制表符
\S === [^\t\x0B\f\r\n]非空白符
\w === [a=zA=Z0-9]单词字母数字下划线
\W === [^a-zA-Z_0-9]非单词字母数字下划线
var str ='hello world 123456 \t \r jirengu \n ruoyu'
undefined
str
"hello world 123456      
 jirengu 
 ruoyu"

看图运行比较好:

\t制表嘛,就是我选中的长的蓝色部分,再看空格\r短的蓝色部分

\n换行嘛。
str
"hello world 123456      
 jirengu 
 ruoyu"
str.search('world')
6
str.match('world')
["world", index: 6, input: "hello world 123456   
 jirengu ↵ ruoyu"]
str.match(/\d/)
["1", index: 12, input: "hello world 123456      
 jirengu ↵ ruoyu"]
str.match(/\d/g)
(6) ["1", "2", "3", "4", "5", "6"]0: "1"1: "2"2: "3"3: "4"4: "5"5: "6"length: 6__proto__: Array(0)
str.match(/\w/g)
(28) ["h", "e", "l", "l", "o", "w", "o", "r", "l", "d", "1", "2", "3", "4", "5", "6", "j", "i", "r", "e", "n", "g", "u", "r", "u", "o", "y", "u"]
str.match(/\W/g)
(10) [" ", " ", " ", "  ", " ", "
", " ", " ", "↵", " "]0: " "1: " "2: " "3: "    "4: " "5: "
"6: " "7: " "8: "↵"9: " "length: 10__proto__: Array(0)
str.match(/./g)
(36) ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", " ", "1", "2", "3", "4", "5", "6", " ", "  ", " ", " ", "j", "i", "r", "e", "n", "g", "u", " ", " ", "r", "u", "o", "y", "u"]
str.match(/ /g)
    (7) [" ", " ", " ", " ", " ", " ", " "]

边界

^            以XXXX开头
$           以XXXX结尾
\b      单词边界
\B     非单词边界
str
"hello world 123456      
 jirengu 
 ruoyu"

str.match(/[^hello]/g)            //有[]匹配的是一个字符
(29) [" ", "w", "r", "d", " ", "1", "2", "3", "4", "5", "6", " ", " ", " ", "
", " ", "j", "i", "r", "n", "g", "u", " ", "↵", " ", "r", "u", "y", "u"]
str.match(/^hello/g)          //没有[]匹配的是以什么什么开头的
["hello"]0: "hello"length: 1__proto__: Array(0)
str
"hello world 123456      
 jirengu 
 ruoyu"
str.match(/hello$/g)
null
var str ='hello world 123456 \t \r jirengu \n ruoyu hello'
undefined
str.match(/hello$/g)
["hello"]0: "hello"length: 1__proto__: Array(0)

var str ='hello1 world hello2 123456 \t \r jirengu \n ruoyu hello3'
undefined
str.match(/hello\d/g)
(3) ["hello1", "hello2", "hello3"]0: "hello1"1: "hello2"2: "hello3"length: 3__proto__: Array(0)
str.match(/hello\d$/g)
["hello3"]
str.match(/^hello/g)
["hello"]0: "hello"length: 1__proto__: Array(0)
str.match(/^hello1/g)
["hello1"]
str.match(/^hello\d/g)
["hello1"]
var str ='hello1 whello9orld hello2 12-hello8-3456 \t \r jirengu \n ruoyu hello3'
undefined
str.match(/\bhello\d\b/g)
(4) ["hello1", "hello2", "hello8", "hello3"]0: "hello1"1: "hello2"2: "hello8"3: "hello3"length: 4__proto__: Array(0)
var str ='hello1 hello9orld hello2 12-hello8-3456 \t \r jirengu \n ruoyu hello3'
undefined
str.match(/\bhello\d/g)
(5) ["hello1", "hello9", "hello2", "hello8", "hello3"]
str.match(/hello\d\b/g)
(4) ["hello1", "hello2", "hello8", "hello3"]
//单词边界可以是空格,可以是特殊字符-,不可以是数字和字母
//单词边界有哪些特殊字符,除了-,\t \r \n呢?
var str ='\thello1 hello9orld hello2 12-hello8-3456 \t \r jirengu \n ruoyu hello3'
undefined
str.match(/\bhello\d\b/g)
(4) ["hello1", "hello2", "hello8", "hello3"]
var str ='\rhello1 hello9orld hello2 12-hello8-3456 \t \r jirengu \n ruoyu hello3'
undefined
str.match(/\bhello\d\b/g)
(4) ["hello1", "hello2", "hello8", "hello3"]
var str ='\nhello1 hello9orld hello2 12-hello8-3456 \t \r jirengu \n ruoyu hello3'
undefined
str.match(/\bhello\d\b/g)
(4) ["hello1", "hello2", "hello8", "hello3"]

小测试:

var str = 'header clearfix active header-fix'
undefined
//找单词header
str.match(/(^|\s)header($|\s)/g)
["header "]

量词

?        出现0次或1次
+       至少出现1次
*           出不出现都可以 
{n}     出n次
{n,m}   出现n到m次
{n,}    至少出现n次
var str='http://jirengu.com'
undefined
var str2='https://jirengu.com'
undefined
str.match(/https?:\/\/.+/)
["http://jirengu.com", index: 0, input: "http://jirengu.com"]
str2.match(/https?:\/\/.+/)
["https://jirengu.com", index: 0, input: "https://jirengu.com"]
var str3='httpsssss://sandan.com'
undefined
str3.match(/https+:\/\/.+/)
["httpsssss://sandan.com", index: 0, input: "httpsssss://sandan.com"]
str.match(/(https?)?:\/\/.+/)
(2) ["http://jirengu.com", "http", index: 0, input: "http://jirengu.com"]0: "http://jirengu.com"1: "http"index: 0input: "http://jirengu.com"length: 2__proto__: Array(0)
var str4='://nsannan.snf'
undefined                //这里的写错了应该没冒号的,不过方法是方法,道理一样的
str4.match(/(https?)?:\/\/.+/)
(2) ["://nsannan.snf", undefined, index: 0, input: "://nsannan.snf"]
str2.match(/(https?)?:\/\/.+/)
(2) ["https://jirengu.com", "https", index: 0, input: "https://jirengu.com"]

如何判断字符串是网址?严谨写法如此:
str.match(/(https?:)?\/\/.+/)
(2) ["http://jirengu.com", "http:", index: 0, input: "http://jirengu.com"]

如何判断电话号码:1开头,共十一位数字,结尾。

var str='19281712837'
undefined
str.match(/^1[3578]\d{9}/)
null
var str2='18825256114'
undefined
str2.match(/^1[3578]\d{9}/)
["18825256114", index: 0, input: "18825256114"]
var str3='aa135666666'
undefined
var str3='aa13566666687'
undefined
str3.match(/^1[3578]\d{9}/)
null
var str4='188252561142382'
undefined
str4.match(/^1[3578]\d{9}/)
["18825256114", index: 0, input: "188252561142382"]
str4.match(/^1[3578]\d{9}$/)
null
str2.match(/^1[3578]\d{9}$/)
["18825256114", index: 0, input: "18825256114"]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,911评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,014评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,129评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,283评论 1 264
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,159评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,161评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,565评论 3 382
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,251评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,531评论 1 292
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,619评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,383评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,255评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,624评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,916评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,199评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,553评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,756评论 2 335

推荐阅读更多精彩内容