如何将正则表达式用于茉莉花匹配器
How to use regex for jasmine matchers
我需要验证文本标签,但它包含动态部分,所以我尝试使用正则表达式,但它不起作用。
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+/'.
- 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"]
您需要将模式转换为字符串,然后您可以使用 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]*$/);
我需要验证文本标签,但它包含动态部分,所以我尝试使用正则表达式,但它不起作用。
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+/'.
- 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"]
您需要将模式转换为字符串,然后您可以使用 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]*$/);