zoukankan      html  css  js  c++  java
  • JS 正则详解

    test - 判断字符串是否符合规定的正则

      rep =/d+/;

      rep.test('211')#true

      rep =/^d+$/

      rep.test('2222ff3') #false

      rep.test('22223') #true

    exec -获取匹配的数据

      req = /d+/

      str="wamhsjem_67_houyufa_21"

      req.exec(str)#["67", index: 9, input: "wamhsjem_67_houyufa_21", groups: undefined]

      

      var text = "JavaScript is more fun than Java or JavaBeans!";

      var pattern = /Javaw*/;

      result = pattern.exec(text)#["JavaScript"]

      var pattern = /Java(w*)/;#加分组

      var pattern = /Javaw*/;#["JavaScript", "Script"]

      var pattern = /(Java)w*/g;#全局匹配

      pattern.exec(text)#第一次拿["JavaScript"],第二次["Java"],第三次["JavaBeans"],第四次是null

    1、定义正则表达式

    /.../  用于定义正则表达式

    /.../g 表示全局匹配

    /.../i 表示不区分大小写

    /.../m 表示多行匹配

    多行匹配例子:

    var text = "JavaScript is more fun than JavaEE or JavaBeans!";

    var pattern = /^Javaw*/g

    pattern.exec(text)#["JavaScript"]

    pattern.exec(text)#null

    var pattern = /^Javaw*/gm;

    pattern.exec(text)#第一次匹配["JavaScript"],第二次匹配["JavaEE"],第三次为null

      

    var pattern = /Javaw*/;
        var text = "JavaScript is more fun than Java or JavaBeans!";
        result = pattern.exec(text)
  • 相关阅读:
    P3350 [ZJOI2016]旅行者
    P4178 Tree
    P2375 [NOI2014]动物园
    P2827 蚯蚓
    1002: [FJOI2007]轮状病毒
    1070: [SCOI2007]修车
    AtCoder Grand Contest 021完整题解
    Running to the End(Codeforces & AtCoder 百套计划)
    SDWC2017游记
    非传统题初探——AtCoder Practice Contest #B
  • 原文地址:https://www.cnblogs.com/anhao-world/p/14471121.html
Copyright © 2011-2022 走看看