【问题标题】:How to use regex for jasmine matchers如何将正则表达式用于茉莉花匹配器
【发布时间】:2016-01-15 18:24:07
【问题描述】:

我需要验证文本标签但它包含动态部分,所以我尝试使用正则表达式但它不起作用。

expect(aboutPage.userInterfaceText.getText()).toMatch('/- User Interface: v \d+\.\d+\.\d+/');

我总是得到下一个错误:

- Expected '- User Interface: v 4.4.63' to match '/- User Interface: v d+.d+.d+/'.

【问题讨论】:

    标签: regex jasmine protractor string-matching


    【解决方案1】:

    - Expected '- User Interface: v 4.4.63' to match '/- User Interface: v d+.d+.d+/'.

    如您所见,模式两端的斜杠也包含在表达式中,但您的 - User Interface: v 4.4.63 测试字符串不包含斜杠。

    不应将正则表达式括在单引号中以使其成为valid regular expression object

    expect(aboutPage.userInterfaceText.getText()).toMatch(/- User Interface: v \d+\.\d+\.\d+/);
    

    在控制台上为我工作:

    > var s = "- User Interface: v 4.4.63";
    > var re = /- User Interface: v \d+\.\d+\.\d+/;
    > s.match(re)
    ["- User Interface: v 4.4.63"]
    

    【讨论】:

      【解决方案2】:

      您需要将模式转换为字符串,然后您可以使用 match() 来检查正则表达式匹配器

      expect(control.errors.pattern.requiredPattern).toString().match('^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\S\./0-9]*$');
      

      或者像这样

      expect(control.errors.pattern.requiredPattern).toString().match(/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\S\./0-9]*$/);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        • 2014-12-12
        • 2013-02-19
        • 2017-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多