JavaScript 正则表达式的学习

目录

  • Preface

  • 语法

  1. 正则表达式的语法
  2. 正则表达式的构建
  3. 正则表达式的修饰符
  4. 正则表达式的方法
  5. 正则表达式的元字符
  6. 正则表达式的量词
  7. 正则表达式的中括号
  • 常用组合

Preface

我受够每次进行数据匹配都重新学习正则和疯狂搜索了!!!

这次一口气把正则表达式总结完毕!!

然后,勤加练习

学习正则表达式,本文足以!

正则表达式是用于匹配字符串中字符组合的模式。在 JavaScript中,正则表达式也是对象。这些模式被用于 RegExp 的 exec 和 test 方法, 以及 String 的 matchmatchAllreplacesearch 和 split 方法。

                                                                                                                                                               —— MDN

正则表达式(英语:Regular Expression,在代码中常简写为regex、regexp或RE)使用单个字符串来描述、匹配一系列符合某个句法规则的字符串搜索模式。 搜索模式可用于文本搜索和文本替换。

                                                                                                                                                               —— 菜鸟教程


语法

1. 语法格式:

/pattern/modifiers(Optional)    -English
/模式匹配样本/修饰符(可选)         -中文

/*
pattern:描述匹配信息
modifiers:修饰匹配信息是否用于区分大小写,全局匹配,多行匹配
    i:不区分大小写
    g:执行全局匹配
    m:执行多行匹配
*/

2. 两种构建方式

let patt = new RegExp(pattern, modifiers);

// 或者
let patt = /pattern/modifiers;

两者等价

2. 构建一个简单的正则表达式

let url = "https://www.nethome.fun/?q=about";
let patt = /nethome.fun/i;
let domain = url.search(patt); // 12

3. 正则表达式的修饰符 (modifiers)

修饰符
修饰符 / modifiers描述 / describe
i全称:case-insensitive ,忽略字符串大小写
g全称:global, 表示在搜索到第一个内容的时候,继续搜索,不停止
m全称:multiline, 表示在搜索时遇到换行,也会继续模式匹配

4. 正则表达式的方法 (methods)

RegExp方法 in String
方法 / methods描述 / describe
Str.match(Reg)返回值:包含匹配字符串的数组,或者null
Str.search(Reg)返回值:匹配字符串首次出现的位置(下标值)
Str.relace(Reg, newStr)返回值:返回被newStr替换的新字符串
Str.split(Reg)返回值:返回按RegExp拆分的数组
let url = "https://www.nethome.fun/?q=about";
let patt = /nethome.fun/i;

// Str.match 模式-匹配
let domainMatch = url.match(patt); //["nethome.fun", index: 12, input: "https://www.nethome.fun/?q=about", groups: undefined]

// Str.search 模式-搜索
let domainSearch = url.search(patt); // 12

// Str.replace 模式-替换
let domainReplace = url.replace(patt, "baidu.com") //"https://www.baidu.com/?q=about"

// Str.split 模式-拆分
let domainSplit = url.split(patt) // ["https://www.", "/?q=about"]

 

5. 正则表达式的元字符(Metacharacter)

元字符
元字符 / Metacharacter描述 / describe
.除了换行和行结束符的所有单个字符
\w单词字符
\W单词字符
\d数字字符
\D数字字符
\s空白字符
\S空白字符
\b单词边界
\B单词边界
\0NULL字符
\n换行符
\f换页符
\r回车符
\t制表符
\v垂直制表符
\xxx以八进制数xxx规定字符
\xdd以十六进制数dd规定的字符
\uxxxx以十六进制数xxxx规定的Unicode字符
let str = "https://www.nethome.fun/?q=about&id=777777";
let patt ; // 声明匹配规则

// . 的使用
patt = /.t/g ; // 匹配后跟字符为t的字符
str.match(patt); // ["ht", "et", "ut"]

// \w 的使用
patt = /\w/g ; // 匹配单词字符
str.match(patt); // ["h", "t", "t", "p", "s", "w", "w", "w", "n", "e", "t", "h", "o", "m", "e", "f", "u", "n", "q", "a", "b", "o", "u", "t", "i", "d"]

// \W 的使用
patt = /\W/g ; // 匹配非单词字符
str.match(patt); // [":", "/", "/", ".", ".", "/", "?", "=", "&", "i", "d", "=", "7", "7", "7", "7", "7", "7"]

// \d 的使用
patt = /\d/g ; // 匹配数字
str.match(patt); // ["7", "7", "7", "7", "7", "7", "7"]

// \D 的使用
patt = /\D/g ; // 匹配非数字字符
str.match(patt); // ["h", "t", "t", "p", "s", ":", "/", "/", "w", "w", "w", ".", "n", "e", "t", "h", "o", "m", "e", ".", "f", "u", "n", "/", "?", "q", "=", "a", "b", "o", "u", "t", "&", "i", "d", "="]

// \s 的使用
patt = /\s/g ; // 匹配空白字符
str.match(patt); // null

// \S 的使用
patt = /\S/g ; // 匹配非空白字符
str.match(patt); // ["h", "t", "t", "p", "s", ":", "/", "/", "w", "w", "w", ".", "n", "e", "t", "h", "o", "m", "e", ".", "f", "u", "n", "/", "?", "q", "=", "a", "b", "o", "u", "t", "&", "i", "d", "=", "7", "7", "7", "7", "7", "7", "7"]

// \b 的使用
patt = /\bhttp/g ; // 匹配单词边界
str.match(patt); // ["http"]

// \B 的使用
patt = /http\B/g ; // 匹配非单词边界
str.match(patt); // ["http"]

// \0 的使用
patt = /\0/g ; // 匹配null字符
str.match(patt); // null

// \n 的使用
patt = /\n/g ; // 匹配换行符
str.match(patt); // null

// \f 的使用
patt = /\f/g ; //匹配换页符
str.match(patt);// null

// \r 的使用
patt = /\r/g ; //匹配回车符
str.match(patt); // null

// \t 的使用
patt = /\t/g ; // 匹配制表符
str.match(patt); // null

// \v 的使用
patt = /\v/g ; // 匹配垂直制表符
str.match(patt); // null

// \xxx 的使用
patt = /\141/g ; // 匹配以xxx(141)规定的八进制字符
str.match(patt); // ["a"]

// \xdd 的使用
patt = /\x61/g ;  // 匹配以xdd(x61)规定的十六进制字符
str.match(patt); // ["a"]

// \uxxxx 的使用
patt = /\u0061/g ;  // 匹配以xxxx(0061)规定的十六进制字符
str.match(patt); // ["a"]

6. 正则表达式的量词

量词
量词描述 / describe
n+

匹配任何至少一个n的字符串

n*匹配任何包含任意个(包含0)n的字符串
n?匹配0个或者1个n的字符串
n{x}匹配连续x个n字符串
n{x, y}匹配至少x个至多y个连续n的字符串
n{x,}匹配至少为x的连续n的字符串
n$匹配任何结尾为n的字符串
^n匹配任何开头为n的字符串
?=n匹配任何后跟为n的字符串
?!n匹配任何后跟不是n的字符串
let str = "https://www.nethome.fun/?q=about&id=777777";
let patt ; // 声明匹配规则

// n+ 的使用
patt = /t+/g; //匹配至少为1个 至多不限制的连续个t的字符串
str.match(patt); // ["tt", "t", "t"]

// n* 的使用
patt = /t*/g; // 匹配至少为0个 至多不限制的连续个t的字符串
str.match(patt); // ["tt", "t", "t"]

// 的使用
patt = /t?/g; // 匹配数0或1个t的字符
str.match(patt); // ["", "t", "t", "", "", "", "", "", "", "", "", "", "", "", "t", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "t", "", "", "", "", "", "", "", "", "", "", "", ""]

// n{x} 的使用
patt = /t{1}/g; // 匹配1个t的字符
str.match(patt); // ["t", "t", "t", "t"]

// n{1,2} 的使用
patt = /t{1,2}/g; //匹配至少为1个 至多2个连续t的字符串
str.match(patt); // ["tt", "t", "t"]

// n{1,} 的使用
patt = /t{1,}/g; // 匹配至少为1个 至多不限制个连续t的字符串
str.match(patt); // ["tt", "t", "t"]

// n$ 的使用
patt = /7$/g; //匹配以"7"结尾的字符串
str.match(patt); // ["7"]

// ^n 的使用
patt = /^http/g; // 匹配以"http"开头的字符串
str.match(patt); // ["http"]

// ?=n 的使用
patt = /7(?=7)/g; // 匹配"7"后跟为7的字符串
str.match(patt); // ["7", "7", "7", "7", "7", "7"]

// ?!n 的使用
patt = /7(?!7)/g; // 匹配"7"后跟不为7的字符串
str.match(patt); // ["7"]

7. 正则表达式的中括号

方括号
表达式描述 / describe
[abc]匹配方括号之间任意单个字符
[^abc]匹配非括号之间的任意字符
[0-9]匹配从0-9之间的字符
[a-z]匹配a-z之间的任意单个小写字母
[A-Z]匹配A-Z之间的任意单个大写字母
[http|id]匹配任何指定的选项

 

let str = "https://www.nethome.fun/?q=about&id=777777";
let patt ; // 声明匹配规则

// [abc] 的使用

patt = /[abc]/g; // 匹配括号内任意一字符
str.match(patt); // ["a", "b"]



// [^abc] 的使用

patt = /[^abc]/g; // 匹配不在括号内的任意一字符
str.match(patt); // ["h", "t", "t", "p", "s", ":", "/", "/", "w", "w", "w", ".", "n", "e", "t", "h", "o", "m", "e", ".", "f", "u", "n", "/", "?", "q", "=", "o", "u", "t", "&", "i", "d", "=", "7", "7", "7", "7", "7", "7"]


// [0-9] 的使用

patt = /[0-9]/g; // 匹配0-9之间的字符
str.match(patt);  // ["7", "7", "7", "7", "7", "7"]


// [a-z] 的使用

patt = /[a-z]/g;  // 匹配a-z之间的字符
str.match(patt);  // ["h", "t", "t", "p", "s", "w", "w", "w", "n", "e", "t", "h", "o", "m", "e", "f", "u", "n", "q", "a", "b", "o", "u", "t", "i", "d"]


// [A-Z] 的使用

patt = /[A-Z]/g; // 匹配A-Z之间的字符
str.match(patt); // null


// [A-z] 的使用

patt = /[0-9]/g; // 匹配A-z之间的字符
str.match(patt); // ["h", "t", "t", "p", "s", "w", "w", "w", "n", "e", "t", "h", "o", "m", "e", "f", "u", "n", "q", "a", "b", "o", "u", "t", "i", "d"]


// [http|id] 的使用

patt = /[http|id]/g; // 匹配选项内的选项
str.match(patt); // ["h", "t", "t", "p", "t", "h", "t", "i", "d"]

常用组合

//对电子邮件的验证
 var email = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;

//身份证号(18位)正则
var cP = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;

/QQ号正则,5至11位
var qq = /^[1-9][0-9]{4,10}$/;
//微信号正则,6至20位,以字母开头,字母,数字,减号,下划线
var wx = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值