簡體   English   中英

使用 JavaScript 從數組中的字符串中刪除所有字母字符和前導零

[英]Remove all alphabetical characters and leading zeros from strings in an array with JavaScript

感謝您花時間閱讀我的問題。

我需要從數組中的字符串元素中刪除所有字母字符前導零。 該數組將如下所示:

["DE73456","DE71238","FR00034","FR00036","ES00038","US00039","DE00098", ...]

結果應該是以下整數數組:

[73456,71238,34,36,38,39,98, ...]

最后,我想從這個數字的升序排序列表中確定最低的省略值 我已經找到了一個 JS 函數,它可以通過傳遞一個整數數組來完成這項工作 - 它可以按預期完美運行。

也許我們可以以某種方式將上述要求與下面的 JS 函數結合起來,以確定最低的省略值。

var k = [73456,71238,34,36,38,39,98];

k.sort(function(a, b) { return a-b; });   // To sort by numeric

var offset = k[0];
var lowest = -1;
for (i = 0;  i < k.length;  ++i) {
  if (k[i] != offset) {
    lowest = offset;
    break;
  }
  ++offset;
}
if (lowest == -1) {
    lowest = k[k.length - 1] + 1;
}
return lowest;

所以,簡而言之,我想從字符串數組中確定最小的省略值

["DE73456","DE71238","FR00034","FR00036","ES00038","US00039","DE00098", ...]

因此,對於上述示例,JS 函數的結果應返回35

非常感謝您的建議! 我期待着閱讀您的評論。

您可以使用這個簡單的正則表達式替換:

repl = str.replace(/^[a-zA-Z]+0*/, "")

正則表達式演示

正則表達式詳情:

  • ^ : 開始
  • [a-zA-Z]+ : 匹配一個字母
  • 0* : 匹配零個或多個0 s
  • 替換只是一個空字符串

您還可以使用單個字符類^[A-Z0]+從字符串^的開頭匹配 1 次或多次出現的 AZ 或 0,然后使用數組映射。

s = s.replace(/^[A-Z0]+/, "")

 const a = ["DE73456", "DE71238", "FR00034", "FR00036", "ES00038", "US00039", "DE00098"]; console.log(a.map(s => s.replace(/^[A-Z0]+/, "")))

您還可以使用match而不是刪除不需要的字符。

str = "FR00034"
str.match(/[1-9][0-9]*/)

正則表達式詳情:

  • [1-9] : 第一個字符應該是非零數字
  • [0-9]* : 以下所有數字

獲得最低值的最終函數應該看起來像這樣。

 function getLowestOf() { const numbers = arr.map((item) => Number(item.match(/[1-9][0-9]*/)[0])); const sorted = numbers.sort((a, b) => a - b); // console.log(sorted); return sorted[0]; } const arr = ["DE73456","DE71238","FR00034","FR00036","ES00038","US00039","DE00098"]; console.log(getLowestOf(arr))

對於提供的列表示例,像 ... (/^[0\\D]+/) ... 這樣的正則表達式已經足夠用於字符串replace 由於此任務以及將字符串轉換為數值的附加任務必須為每個數組項處理,因此需要使用Array.prototype.map

為了實現 OP 希望的最終結果,必須實現一個函數,該函數從整數的無序列表中獲取最小的省略值......

 const sampleList = ["DE73456","DE71238","FR00034","FR00036","ES00038","US00039","DE00098"]; // for the above samples a regex like ... /^[0\\D]+/ ... already // is sufficient enough ... [https://regex101.com/r/ZSDGvC/1/] // an unsorted list of integers ... console.log( sampleList .map(sample => Number(sample.replace((/^[0\\D]+/), ''))) ); function getLowestOmittedValueFromIntegerList(list) { let lowestOmittedInteger; let hasOmittedValue = false; const maximumListCount = (list.length - 1); // use shallow copy for not mutating the original // ... and sort this new array in ascending order. list = Array.from(list).sort((a, b) => (a - b)); // look for omitted value, save it and exit early. list.some((integer, idx) => { if (idx < maximumListCount) { if ((integer + 1) < list[idx + 1]) { lowestOmittedInteger = (integer + 1); hasOmittedValue = true; } } return hasOmittedValue; }); return lowestOmittedInteger; } // the lowest omitted value from an unsorted list of integers ... console.log( getLowestOmittedValueFromIntegerList( sampleList .map(sample => Number(sample.replace((/^[0\\D]+/), ''))) ) ); // proof of concept ... console.log('\\nproof of concept ...\\n\\n'); console.log( // expect the undefined value getLowestOmittedValueFromIntegerList([9,8,7,6,6,6,5,4,3,2,1,0]) ); console.log( // expect 7 getLowestOmittedValueFromIntegerList([9,8,6,6,6,5,4,3,2,1,0]) ); console.log( // expect 11 getLowestOmittedValueFromIntegerList([0,1,2,3,4,5,6,7,8,9,10,12]) ); console.log( // expect the undefined value getLowestOmittedValueFromIntegerList([0,1,2,3,4,5,6,7,8,9,10,11,12]) ); console.log( // expect -1 getLowestOmittedValueFromIntegerList([-4,-3,-2,0,1,2,3,4]) ); console.log( // expect -2 getLowestOmittedValueFromIntegerList([-4,-3,-1,0,1,2,3,4]) );
 .as-console-wrapper { min-height: 100%!important; top: 0; }

暫無
暫無

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

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