How to use regex for jasmine matchers

13,365

Solution 1

- 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"]

Solution 2

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]*$/);
Share:
13,365
iPogosov
Author by

iPogosov

Updated on June 23, 2022

Comments

  • iPogosov
    iPogosov about 2 years

    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+/'.