其他分享
首页 > 其他分享> > 正则表达式

正则表达式

作者:互联网

语法

/正则表达式主体/修饰符
var str = "Visit Runoob!";
var n = str.search(/Runoob/i);
// n=6

replace()

var str = 'Visit Microsoft!'
var txt = str.replace(/microsoft/i,"Runoob");
// txt = Visit Runoob!

修饰符

模式

元字符(元字符是拥有特殊含义的字符)

量词

懒惰限定符

使用RegExp对象

test()

test() 方法用于检测一个字符串是否匹配某个模式,如果字符串中含有匹配的文本,则返回 true,否则返回 false。

var patt = /e/;
const res = patt.test("The best things in life are free!");
// res = true

exec()

exec() 方法用于检索字符串中的正则表达式的匹配。

该函数返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。

const res = /ee+/.exec("The best things in life are free!");
// res = ['ee', index: 30, input: 'The best things in life are free!', groups: undefined]

标签:字符,匹配,正则表达式,res,尽可能少,var,字符串
来源: https://www.cnblogs.com/JianXin1994/p/16188513.html