无法检查字符串中的数字和符号。

huangapple go评论53阅读模式
英文:

can't check string for digits and symbols

问题

我需要检查字符串的三种情况:

  1. 字符串只包含数字;
  2. 字符串只包含字符;
  3. 字符串包含数字和字符。

我编写了一个函数,但它不能正确工作。有人可以帮助我吗?
我使用的是JS+wdio。

async function checkNumber() {
  const string = (await homepage.getStringText()).slice(15);

  expect(await homepage.getStringText()).not.equal("unknown");

  if (string.match(/[0-9]/) != null) {
    expect(string.length).to.be.greaterThanOrEqual(3);
    expect(string.length).to.be.lessThanOrEqual(14);
  }
  if (string.match(/[A-Z, a-z]/) != null) {
    expect(string).to.equal("n/a");
  }
  if (string.match(/[A-Z, a-z] + [0-9]/) != null) {
    expect(string.length).to.be.greaterThanOrEqual(10);
    expect(string.length).to.be.lessThanOrEqual(17);
  }
}

希望这可以帮助您解决问题。

英文:

I need to check string with 3 cases:

  1. string contain only digits;

  2. string contain only characters;

  3. string contain digits + characters;

I wrote function, but it dont worc correctly. Can somebody help me with it?
I use JS+wdio

async function checkNumber() {
  const string = (await homepage.getStringText()).slice(15);

  expect(await homepage.getStringText()).not.equal("unknown");

  if (string.match(/[0-9]/) != null) {
    expect(string.length).be.greaterThanOrEqual(3);
    expect(string.length).be.lessThanOrEqual(14);
  }
  if (string.match(/[A-Z, a-z]/) != null) {
    expect(string).equal("n/a");
  }
  if (string.match(/[A-Z, a-z] + [0-9]/) != null) {
    expect(string.length).be.greaterThanOrEqual(10);
    expect(string.length).be.lessThanOrEqual(17);
  }
}

答案1

得分: 1

  1. 字符串仅包含数字:/^\d+$/
  2. 字符串仅包含字符:/^\D+$/
  3. 字符串包含数字和字符:/^\w+$/

要了解更多关于正则表达式的信息,请阅读MDN文档

英文:

I hope it can help you:

  1. string contain only digits: /^\d+$/
  2. string contain only characters: /^\D+$/
  3. string contain digits + characters: /^\w+$/

To learn more about Regex, read the MDN documentation

huangapple
  • 本文由 发表于 2023年6月1日 04:10:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定