如何检查字符串中某些字符的存在和数量(例如 'x'/'X' 和 'o'/'O')?

How to check existence and amount of certain characters within a string (e.g. 'x'/'X' and 'o'/'O')?

我参加了代码大战(基础知识),有一个套路要求我制作一个程序,让您可以查看 'x''o' 的数量是否相同(例如'xXXooO' 应该 return true'xXxxO' 应该 return false).

我利用我最好的编码知识(有点)构建了这段代码,但它不起作用。

function XO(str) {
  let string = str;
  const o = string.match(/O/g) + string.match(/o/g);
  const x = string.match(/X/g) + string.match(/x/g);
  if (o.length != x.length) {
    return true;
  } else {
    return false;
  }
}

请告诉我我的代码有什么问题。

这部分是我更新后的

我更新了我的代码,但它说 null 不是一个对象。我也更新了变量,但我不认为这是原因。

function XO(str) {
  let string = str;
  const largeO = string.match(/O/g);
  const smallO = string.match(/o/g);
  const largeX = string.match(/X/g);
  const smallX = string.match(/x/g);
  const oCombined = largeO.length + smallO.length;
  const xCombined = largeX.length + smallX.length;
  if (oCombined = xCombined) {
    return true;
  } else {
    return false;
  }
}
console.log(XO("OxX"));

对于 'x'/'X''o'/'O' 的不区分大小写的匹配,需要使用正则表达式的 i 修饰符(或标志) .

当然需要处理失败 match method which for the next provided example code is taken care of by Optional chaining / ?. and the Nullish coalescing operator / ??null return 值。

function isXAndOWithSameAmount(value) {
  // always assure a string type  
  value = String(value);

  // in case of a `null` value ... ...count is -1.
  const xCount = value.match(/x/gi)?.length ?? -1;

  // in case of a `null` value ... ...count is -2.
  const oCount = value.match(/o/gi)?.length ?? -2;

  // thus the return value for neither an 'x'/`X`
  // match nor an 'o'/`O` match will always be `false`.
  return (xCount === oCount);

  // in order to achieve another (wanted/requested)
  // behavior one needs to change both values after
  // the nullish coalescing operator accordingly.
}

console.log(
  "isXAndOWithSameAmount('xXXooO') ..?",
  isXAndOWithSameAmount('xXXooO')
);
console.log(
  "isXAndOWithSameAmount('xXoXooOx') ..?",
  isXAndOWithSameAmount('xXoXooOx')
);

console.log(
  "isXAndOWithSameAmount('xXxxO') ..?",
  isXAndOWithSameAmount('xXxxO')
);
console.log(
  "isXAndOWithSameAmount('xOoXxxO') ..?",
  isXAndOWithSameAmount('xOoXxxO')
);

console.log(
  "isXAndOWithSameAmount('') ..?",
  isXAndOWithSameAmount('')
);
console.log(
  "isXAndOWithSameAmount('bar') ..?",
  isXAndOWithSameAmount('bar')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

其中一个原因可以通过将 match 的结果和可选链接的 ?.length 通过 parseInt which will return either an (integer) number value grater than zero or the NaN value.

转换为整数值来实现相同的行为

function isXAndOWithSameAmount(value) {
  // always assure a string type  
  value = String(value);

  // making use of optional chaining and `parseInt`
  // which will result in (integer) number values
  // grater than zero or the `NaN` value.
  const xCount = parseInt(value.match(/x/gi)?.length);
  const oCount = parseInt(value.match(/o/gi)?.length);

  // since a `NaN` value is not equal to itself
  // the return value for neither an 'x'/`X` match
  // nor an 'o'/`O` match will always be `false`.
  return (xCount === oCount);
}

console.log(
  "isXAndOWithSameAmount('xXXooO') ..?",
  isXAndOWithSameAmount('xXXooO')
);
console.log(
  "isXAndOWithSameAmount('xXoXooOx') ..?",
  isXAndOWithSameAmount('xXoXooOx')
);

console.log(
  "isXAndOWithSameAmount('xXxxO') ..?",
  isXAndOWithSameAmount('xXxxO')
);
console.log(
  "isXAndOWithSameAmount('xOoXxxO') ..?",
  isXAndOWithSameAmount('xOoXxxO')
);

console.log(
  "isXAndOWithSameAmount('') ..?",
  isXAndOWithSameAmount('')
);
console.log(
  "isXAndOWithSameAmount('bar') ..?",
  isXAndOWithSameAmount('bar')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }