You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
第一种方式
var reg = /\bis\b/;
var str = 'This is abc,This is def';
str.replace(reg,'IS');
var reg = /\bis\b/g;
var str = 'This is abc,This is def';
str.replace(reg,'IS');
第二种方式
var reg = new RegExp('\\bis\\b')
var str = 'This is abc,This is def';
str.replace(reg,'IS');
var reg = new RegExp('\\bis\\b','g')
var str = 'This is abc,This is def';
str.replace(reg,'IS');
正则表达式小结
实例化一个RegExp对象有俩种方式
/^$/ 和 /^$/g 这俩种方式的区别? 后面一种方式表示匹配全文,前面只会匹配第一项
g标示全局标示
i表示忽略大小写
m表示多行搜索
使用时可以连在一起使用;例如 /\bis\b/gi 表示全局搜索并且忽略大小写
###常用元字符
\d 数字标示
\D 除数字以外
\w 单词表示 包括字母数字下划线
\W 除了上面\w以外的
\s 空格
\S 除了空格
\b 单词边界
\B 非单词边界 【单词边界分左右】 例如想匹配This中的is \Bis\b
{} 量词
() $1 分组
[] 或者
^ 开头 取反意思
$ 结尾
. 任意字符
? 0次或一次
贪婪模式、非贪婪模式
贪婪模式是尽可能匹配多的元素
非贪婪模式匹配最少量的元素,只要在量词后面加问号就可以了
例子:
'a12345'.match(/[a-z]\d{3,6}/)
贪婪模式下匹配结果为:'a12345'
'a12345'.match(/[a-z]\d{3,6}?/)
非贪婪模式下匹配结果为:'a123'
The text was updated successfully, but these errors were encountered: