Search code examples
regexjasmineprotractorstring-matching

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

Solution

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