JavaScript中的正则

JavaScript 中的正则

第一步: 定义正则表达式

1
let regex = /\d+/g

第二步: 使用正则表达式

1
2
3
4
5
6
7
let str = "a13adddda3afsdfsfa99aa000aa999a34567"
str.match(regex) // 返回数组
str.matchAll(regex) // 返回迭代器
str.test(regex)
str.search(regex)
str.replace(regex)
str.split(regex)

正则标志:

/g 全局搜索

/i 不区分大小写

/m 多行匹配

\r 代表回车,也就是打印头归位

\n 代表换行,就是走纸

Linux 只用\n 换行

win 下用\r\n 表示换行