字符串变量不接受“?”符号
String variable is not accepting "?" sign
我编写了代码:
element(by.className('charge')).getText()
.then(function(text){
var blabla = "Is this my string?";
expect(text.match(blabla)).toBe(true);
console.log(text);
});
甚至我的控制台输出等于我的 blabla 变量,
我得到结果:
Expected [ 'Is this my string' ] to be true.
没有任何“?”签名。
怎么可能?
match
的参数是一个 正则表达式 ,其中 ?
具有特殊含义。您的意思可能是 toEqual()
而不是:
expect(element(by.className('charge')).getText()).toEqual("Is this my string?");
如果你想要一个正则表达式匹配,创建一个正则表达式对象并使用toMatch()
:
var blabla = /Is this my string\?/;
expect(element(by.className('charge')).getText()).toMatch(blabla);
请注意,在量角器中 expect()
是 "patched" 来隐式解决承诺,您不需要使用 then()
.
match
的参数是:
A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).
所以不要给它传递一个字符串。明确地向它传递一个正则表达式对象(因为将字符串转换为正则表达式并且必须处理两个级别的语法才能通过)。
正则表达式将 ?
视为特殊字符(在您的代码上下文中,它表示 "The g
should appear 0 or 1 time"。如果要匹配它们,您需要转义问号。
var blabla = /Is this my string\?/;
就是说,如果你想匹配整个字符串,直接做测试会更容易:
var blabla = "Is this my string?";
expect(text).toBe(blabla);
您可能误解了 match
方法在 JS 字符串中的作用:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/match
它基本上会使用正则表达式来 return 匹配的组,所以,在这种情况下
< ("Is this my string").match("Is this my string?");
> ["Is this my string"]
答案正确。你想要做的只是比较字符串,只需做:
< "Is this my string" === "Is this my string?";
> false
请注意,它与您使用的测试引擎无关(我不知道),但是可能有比[=16更好的方法=]
expect(text === blabla).toBe(true);
有些东西
expect(text, blabla).toBeEqual();
所以错误信息很漂亮 ;)
提供给 match()
的字符串参数是一个正则表达式,这里的 ?
表示 "match the previous zero or one times"。它在你的例子中做了什么:-)
您可以通过写 \?
明确地转义问题,在这种情况下,行为将如您所愿。
干杯,
我编写了代码:
element(by.className('charge')).getText()
.then(function(text){
var blabla = "Is this my string?";
expect(text.match(blabla)).toBe(true);
console.log(text);
});
甚至我的控制台输出等于我的 blabla 变量, 我得到结果:
Expected [ 'Is this my string' ] to be true.
没有任何“?”签名。
怎么可能?
match
的参数是一个 正则表达式 ,其中 ?
具有特殊含义。您的意思可能是 toEqual()
而不是:
expect(element(by.className('charge')).getText()).toEqual("Is this my string?");
如果你想要一个正则表达式匹配,创建一个正则表达式对象并使用toMatch()
:
var blabla = /Is this my string\?/;
expect(element(by.className('charge')).getText()).toMatch(blabla);
请注意,在量角器中 expect()
是 "patched" 来隐式解决承诺,您不需要使用 then()
.
match
的参数是:
A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).
所以不要给它传递一个字符串。明确地向它传递一个正则表达式对象(因为将字符串转换为正则表达式并且必须处理两个级别的语法才能通过)。
正则表达式将 ?
视为特殊字符(在您的代码上下文中,它表示 "The g
should appear 0 or 1 time"。如果要匹配它们,您需要转义问号。
var blabla = /Is this my string\?/;
就是说,如果你想匹配整个字符串,直接做测试会更容易:
var blabla = "Is this my string?";
expect(text).toBe(blabla);
您可能误解了 match
方法在 JS 字符串中的作用:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/match
它基本上会使用正则表达式来 return 匹配的组,所以,在这种情况下
< ("Is this my string").match("Is this my string?");
> ["Is this my string"]
答案正确。你想要做的只是比较字符串,只需做:
< "Is this my string" === "Is this my string?";
> false
请注意,它与您使用的测试引擎无关(我不知道),但是可能有比[=16更好的方法=]
expect(text === blabla).toBe(true);
有些东西
expect(text, blabla).toBeEqual();
所以错误信息很漂亮 ;)
提供给 match()
的字符串参数是一个正则表达式,这里的 ?
表示 "match the previous zero or one times"。它在你的例子中做了什么:-)
您可以通过写 \?
明确地转义问题,在这种情况下,行为将如您所愿。
干杯,