簡體   English   中英

如何檢查字符串中某些字符的存在和數量(例如'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'應該返回true'xXxxO'應該返回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 不是 object。我也更新了變量,但我認為這不是原因。

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方法的null返回值,下一個提供的示例代碼由可選鏈接 / ?. Nullish 合並運算符 / ?? .

 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轉換為 integer 值來實現相同的行為,這將返回一個大於零的(整數)數值或NaN

 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; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM