0%

正则表达式

1
2
3
let testRegex = /Code/
testRegex.test(testStr);
// Returns true
  • 使用|匹配多个规则

    1
    /yes|no|maybe/

  • 使用i忽略大小写

    1
    /ignorecase/i

  • .match()方法提取匹配项

    1
    2
    3
    4
    let ourStr = "Regular expressions";
    let ourRegex = /expressions/;
    ourStr.match(ourRegex);
    // Returns ["expressions"]

  • 使用g多次匹配

    1
    2
    3
    let repeatRegex = /Repeat/g;
    testStr.match(repeatRegex);
    // Returns ["Repeat", "Repeat", "Repeat"]

  • 使用.匹配任意一个字符

  • 使用[]单个字符匹配多种可能性

    1
    /b[aiu]g/

  • 使用-单个字符匹配连续范围

    1
    /[a-e]at/

看到这里的姐妹一看就要暴富暴美,为什么不让这一天提前一点呢ヾ(≧▽≦*)o