正则表达式

正则表达式

参考文章 @CUGGZ
参考文章 @轩陌

声明一个正则表达式

字面量声明

const rex = /pattern/;

构造函数声明

const rex = new RegExp(pattern);

匹配模式

字符集合

[] 可以匹配中括号中包含的任意字符
比如 想要匹配 bt ct

let rex = /[bc]t/g;

let string = "actionbatbtctat";

console.log(string.match(rex)); // [ 'ct', 'bt', 'ct' ]

拓展

[a-z] 从小写a-z
[A-Z] 大写A-Z
[0-9] 0-9
[^a] 除了a以外的

符号范围:[#$%&@];
混合范围:[a-zA-Z0-9],匹配所有数字、大小写字母中的任意字符。

量词

{} 规定一个规则匹配多少次

let rex = /[a-z]{2}/g;

let string = "actionbbatbcctctat";

console.log(string.match(rex)); // [ 'ac', 'ti', 'on', 'bb', 'at', 'bc', 'ct', 'ct', 'at' ]

拓展

{3} 出现次数为最多3{1,4} 1 <= 出现次数 <= 4
{1,} 最少出现1次,简写 +
{0,} 至少0次,简写*
{0,1} 最少0次,最多1次,简写?

元字符

元字符匹配模式
\d相当于[0-9],匹配任意数字;
\D相当于[^0-9];
\w相当于[0-9a-zA-Z],匹配任意数字、大小写字母和下划线;
\W相当于:[^0-9a-zA-Z];
\s相当于[\t\v\n\r\f],匹配任意空白符,包括空格,水平制表符\t,垂直制表符\v,换行符\n,回车符\r,换页符\f;
\S相当于[^\t\v\n\r\f],表示非空白符。

特殊字符

特殊字符匹配模式
.匹配除了换行符之外的任何单个字符;
\将下一个字符标记为特殊字符、或原义字符、或向后引用、或八进制转义符;
|逻辑或操作符;
[^]取非,匹配未包含的任意字符。

位置匹配

位置匹配匹配模式
\b匹配一个单词边界,也就是指单词和空格间的位置;
\B匹配非单词边界;
^匹配开头,在多行匹配中匹配行开头;
$匹配结尾,在多行匹配中匹配行结尾;
(?=p)匹配 p 前面的位置;
(?!=p)匹配不是 p 前面的位置。

修饰符

修饰符描述
g表示全局模式,即运用于所有字符串;
i表示不区分大小写,即匹配时忽略字符串的大小写;
m表示多行模式,强制 $ 和 ^ 分别匹配每个换行符。

实例方法

test() 和 exec()

test() : 检测一个字符串中是否包含 正则表单式的内容. 包含 返回true, 不包含 返回 false.

const regex1 = /a/gi;
const regex2 = /hello/gi;
const str = "Action speak louder than words";

console.log(regex1.test(str)); // true
console.log(regex2.test(str)); // false

exec() : 将字符串中符合正则表达式的内容 返回一个数组,如果没有匹配到内容则返回null.

const regex1 = /a/gi;
const regex2 = /hello/gi;
const str = "Action speak louder than words";

console.log(regex1.exec(str)); // ['A', index: 0, input: 'Action speak louder than words', groups: undefined]
console.log(regex2.exec(str)); // null

字符串方法

search()

用于查找 与正则表达式匹配的内容,并返回其索引.
注意: 1. 只能匹配到第一个 2. 匹配不到的会返回-1

const regex1 = /a/gi;
const regex2 = /p/gi;
const regex3 = /m/gi;
const str = "Action speak louder than words";

console.log(str.search(regex1)); // 输出结果:0
console.log(str.search(regex2)); // 输出结果:8
console.log(str.search(regex3)); // 输出结果:-1

match()

将匹配到的内容 以数组的形式返回.
注意:1. 如果添加了 g 修饰符,可以将所有匹配到的内容放在一个数组中返回 ,没添加的话只会返回第一个匹配到的内容 2. 匹配不到的话 会返回一个 null

const regex1 = /a/gi;
const regex2 = /a/i;
const regex3 = /m/gi;
const str = "Action speak louder than words";

console.log(str.match(regex1)); // 输出结果:['A', 'a', 'a']
console.log(str.match(regex2)); // 输出结果:['A', index: 0, input: 'Action speak louder than words', groups: undefined]
console.log(str.match(regex3)); // 输出结果:null

replace()

用于将匹配到的内容 替换为其他内容

const regex = /a/g;
const str = "Action speak louder than words";

console.log(str.replace(regex, "A")); // 输出结果:Action speAk louder thAn words

正则表达式 使用技巧

限制输入内容(数字)长度

/^[0-9]{2}$/

表达式:

  • ^ 表示 以0-9开头 匹配两次
  • [0-9] 表示 匹配的内容为 0-9
  • {2} 表示匹配的次数(长度)
  • $ 表示 匹配结尾
let s2 = "123";
let arr = s2.match(/^[0-9]{2}$/);
console.log(arr); // null

let s2 = "12";
let arr = s2.match(/^[0-9]{2}$/);
console.log(arr); // [ '12', index: 0, input: '12', groups: undefined ]

正则表达式使用变量

参考文章@蒋不会

var param = 3;
var reg = new RegExp("^[0-9]+"+param+"[a-z]+$","g");//得到:/^[0-9]+3[a-z]+$/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值