量角器:将字符串与大于Jasmine匹配器进行比较

量角器:将字符串与大于Jasmine匹配器进行比较,jasmine,protractor,Jasmine,Protractor,我试图比较: string1=‘客户90有限责任公司’ string2=‘客户501有限责任公司’ 使用: expect(client[i]).toBeGreaterThan(client[i-1]); 失败是因为compare函数认为90大于501,我假设是因为它进行逐字符比较。有没有一种方法可以比较整数?因为web应用程序在string1之后列出string2,因为501大于90 更新:这些字符串没有特定的格式。可能是 'client abc' 'client 90 llc' 'clien

我试图比较:

string1=‘客户90有限责任公司’

string2=‘客户501有限责任公司’

使用:

expect(client[i]).toBeGreaterThan(client[i-1]);
失败是因为compare函数认为90大于501,我假设是因为它进行逐字符比较。有没有一种方法可以比较整数?因为web应用程序在string1之后列出string2,因为501大于90

更新:这些字符串没有特定的格式。可能是

'client abc'
'client 90 llc'
'client 501 llc'
'abcclient'
'client111'
'client 22'
'33client'

如果您知道字符串的格式,可以在的帮助下提取值。在你的例子中,你想在字符串中间提取一个不同的数字,它有共同的部分。以下正则表达式可能有效:

/^client (\d+) llc$/
  • ^
    -字符串的开头
  • ()
    -捕获特定的字符组
  • \d
    -表示数字(0-9),需要反斜杠,因为它是字符序列,与字母
    d
  • +
    -字符可能出现1次或多次
  • $
    -字符串结尾
因此,我们可以在字符串中间找到一组数字。您可以创建一个实用函数来提取值:

function extractNumber(string) {
    var pattern = /^client (\d+) llc$/;
    var match = string.match(pattern);
    if (match !== null) {
        // return the value of a group (\d+) and convert it to number
        return Number(match[1]); 
        // match[0] - holds a match of entire pattern
    }
    return null; // unable to extract a number
}
并在测试中使用它:

var number1 = extractNumber(string1); // 90
var number2 = extractNumber(string2); // 501
expect(number1).toBeGreaterThan(number2);

是的,Jasmine做了一个基于角色的比较。一种方法是将字符串拆分为多个部分,然后仅按如下所示比较数字-

string1 = 'client 90 llc';
string2 = 'client 501 llc';
var newString1 = parseInt(string1.substring(string1.indexOf(' '), string1.lastIndexOf(' ')));
var newString2 = parseInt(string2.substring(string2.indexOf(' '), string2.lastIndexOf(' ')));
expect(newString2).toBeGreaterThan(newString1); //501 > 90 - should return true

我假设您的字符串模式与上面代码段中提到的相同。或者可以使用正则表达式代替substring()函数并获得结果。希望这能有所帮助。

根据您的最新更新,您希望比较什么?它是字符串中的一个数字吗?