Regular Expression 简写 RegExp 正则表达式
Regular Expression 简写 RegExp 正则表达式
Regular Expression 简写 RegExp 正则表达式

正则表达式中的特殊字符

  1. (小数点)默认匹配除换行符之外的任何单个字符

    1
    2
    3
    > (小数点)默认匹配除换行符之外的任何单个字符。
    > 例如,/.n/ 将会匹配 "nay, an apple is on the tree" 中的 'an' 和 'on',但是不会匹配 'nay'。
    >
  2. xxx

  3. xxx

  4. xxx

基本常见

1
2
3
4
// ^  开始位置  $ 结束位置
// [具体内容]{次数}

^[a-z0-9_-]{3,15}$

正则

JavaScript为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/^[0-9]*$/g.test("123456798_fei"); // false
/^[0-9]*/g.test("123456798_fei"); // true


"123".match(/^[0-9]*$/g); // ["123"]
"123".match(/^\d*$/g); // ["123"]
"123_fei".match(/^[0-9]*$/g); // null


"123_fei_456_foo_789".match(/\d{3}/g); // ["123", "456", "789"]
"123_fei_456_foo_789".match(/[0-9]{3}/g); // ["123", "456", "789"]
"12_fei_34 56_fei_78 0_fei_9".match(/\d{1,3}/g); // ["12", "34", "56", "78", "0", "9"]


"123@qq.com---456@gmail.com".match(/\d*@[\w]+[.]{1}\w+/g);// ["123@qq.com", "456@gmail.com"]
1
2
3
4
5
6
7
8
"123_fei_456_foo_789".replace(/[0-9]{3}/g,",");   
",_fei_,_foo_,"

"123_fei_456_foo_789".replace(/(?=[0-9]{3})/g,","); // (?=pattern)
",123_fei_,456_foo_,789"

"123_fei_456_foo_789".replace(/(?=\B[0-9]{3})/g,","); // \B
"123_fei_,456_foo_,789"

正则

手机号变*

1
2
// 中间4为变为*号
"13685468080".replace(/^(\d{3})\d{4}(\d{4})$/,"$1****$2")

去除空格

1
"bar  ".replace(/(^\s*)|(\s*$)/g, "")

前后不能是空格

1
2
3
4
5
6
7
8
// 非空白字符,其S是大写, 
// 前后没有空格返回true

/^\S.*\S$|(^\S{0,1}\S$)/.test("123") // true
/^\S.*\S$|(^\S{0,1}\S$)/.test("1 23") // true

/^\S.*\S$|(^\S{0,1}\S$)/.test(" 123") // false
/^\S.*\S$|(^\S{0,1}\S$)/.test("123 ") // false

匹配二级目录

1
2
3
4
5
6
let dirname = window.location.pathname;
let arr = dirname.match(/(\/.*\/)/g);
console.log(arr);
if (arr) { // 如果是二级目录
console.log(arr[0].split("/"));
}

$符号正则(插入语)

插入语: 任何正则表达式的插入语都会使这部分匹配的副字符串被记忆

插入语

1
2
3
4
5
6
7
8
9
10
11
// $1,$2,$3... 可以保留原来的内容
let str_fei = '欢迎[[123]]登录大飞[[135管理系统,初次密码为[[456]],请放心使用!'
// str_fei = str_fei.replace(/(\d+)/g,'=abc')
// str_fei = str_fei.replace(/(\[\[\d+)/g,'abc')
// str_fei = str_fei.replace(/(\[\[\d+\]\])/g,'abc')

// str_fei = str_fei.replace(/(\d+)/g,'$1_abc')
// str_fei = str_fei.replace(/(\[\[\d+)/g,'$1_abc')
str_fei = str_fei.replace(/(\[\[\d+\]\])/g,'$1_abc')

console.log(str_fei);
1
2
3
4
5
// 将yyyy-mm-dd格式转换为年月日格式
let str_fei2 = "2023-05-01"
let reg =/(\d{4})\-(\d{2})\-(\d{2})/;
let foo = str_fei2.replace(reg,"$1年$2月$3日")
console.log(foo);

可变正则

1
2
3
4
//官方原话: 以下三种表达式都会创建相同的正则表达式:
/ab+c/i; //字面量形式
new RegExp('ab+c', 'i'); // 首个参数为字符串模式的构造函数
new RegExp(/ab+c/, 'i'); // 首个参数为常规字面量的构造函数

当表达式被赋值时,字面量形式提供正则表达式的编译(compilation)状态,当正则表达式保持为常量时使用字面量。例如当你在循环中使用字面量构造一个正则表达式时,正则表达式不会在每一次迭代中都被重新编译(recompiled)。

而正则表达式对象的构造函数,如 new RegExp('ab+c') 提供了正则表达式运行时编译(runtime compilation)。如果你知道正则表达式模式将会改变,或者你事先不知道什么模式,而是从另一个来源获取,如用户输入,这些情况都可以使用构造函数。

1
2
3
4
5
6
7
8
9
10
// demo:
// 以下三种等效
"123".match(/^[0-9]*$/g); // ["123"]
"123".match(new RegExp(/^[0-9]*$/, 'g')); // ["123"]
"123".match(new RegExp("^[0-9]*$", 'g')); // ["123"]

// 以下三种等效
"abbbc".match(/ab+c/g); // ['abbbc']
"abbbc".match(new RegExp(/ab+c/,'g')); // ['abbbc']
"abbbc".match(new RegExp("ab+c",'g')); // ['abbbc'], 写成这样就可以动态拼接正则
1
2
3
4
5
6
7
8
9
10
let str ='<span class="{width} {fei} {class}" data-tip="{width} {fei} {class}">替换花括号中内容</span>'
let dataFei = {
fei: '替换',
class: "small",
width: 10,
};
for (let k in dataFei) {
str = str.replace(RegExp('{' + k + '}', 'g'), dataFei[k] == null ? '' : dataFei[k]);
}
console.log(str);//<span class="a1 10 替换 small" data-tip="a2 10 替换 small">替换花括号中内容</span>

不能有某个字符

1
/^[^y]+$/.test("string") // 没有字符y返回true
1
/^[^\*]+$/.test("string") // 没有字符*号返回true

常用正则

  1. 前后不能有空格

    1
    /^\S.*\S$|(^\S{0,1}\S$)/
  2. 密码由英文大小写和数字组成

    1
    /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+$/
  3. 只能是正整数

    1
    /^\d+$/
  4. 只能是正整数 或 保留两位的小数

    1
    /^\d+(\.\d{0,2})?$/
  5. 只能输入英文和数字

    1
    /^[a-zA-Z0-9]+$/
  6. 只能输入中文

    1
    /^[\u4E00-\u9FA5]+$/
  7. 只能输入英文

    1
    /^[a-zA-Z]+$/
  8. 正确的手机号码

    1
    /^1[0-9]{10,}/
  9. 银行卡号

    1
    /^([1-9]{1})(\d{14}|\d{18})$/
  10. xx

    1
     
  11. xx

  12. xx

  13. xx

  14. xx

  15. xx

  16. xx

  17. xx

  18. xx

  19. xx

  20. xx

常用正则表达式

正则_菜鸟教程

MDN_正则表达式

MDN_RegExp(正则表达式)