Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use regex for jasmine matchers

I need to verify the text label but it contains dynamic part, so I try to use regex but it doesn't work.

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

I always get next error:

- Expected '- User Interface: v 4.4.63' to match '/- User Interface: v d+.d+.d+/'.
like image 546
iPogosov Avatar asked Jan 15 '16 18:01

iPogosov


2 Answers

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

As you can see the slashes at both ends of the pattern are also included into the expression, but your - User Interface: v 4.4.63 test string does not contain the slashes.

You should not enclose the regular expression in the single quotes to make it a valid regular expression object:

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

Works for me on the console:

> 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"]
like image 106
alecxe Avatar answered Sep 22 '22 14:09

alecxe


You need to convert the pattern to string then you can use the match() to check the regex matchers

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

or like this

expect(control.errors.pattern.requiredPattern).toString().match(/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\S\./0-9]*$/);
like image 34
Shabeer M Avatar answered Sep 18 '22 14:09

Shabeer M