Search code examples
javascriptstringvariablesprotractorsign

String variable is not accepting "?" sign


I made code with:

element(by.className('charge')).getText()
    .then(function(text){
        var blabla = "Is this my string?";

        expect(text.match(blabla)).toBe(true);
        console.log(text);
    });

And even is output of my console equal to my blabla variable, I'm getting result:

Expected [ 'Is this my string' ] to be true.

without any "?" sign.

How is it possible?


Solution

  • The argument for match is:

    A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

    So don't pass it a string. Explicitly pass it a regular expression object (since that involves much less pain that converting strings in to regex and having to deal with two levels of syntax to escape through).

    Regular expressions treat ? as a special character (in the context of your code it means "The g should appear 0 or 1 time". You need to escape question marks if you want to match them.

    var blabla = /Is this my string\?/;
    

    That said, if you want to match the whole string, it would be easier to just make that the test:

    var blabla = "Is this my string?";
    expect(text).toBe(blabla);