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

Viewed 323

Thanks for taking your time to read my question.

I need to remove all alphabetical characters and leading zeros from string elements in an array. The array will look something like this:

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

The result should be the following array of integers:

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

Eventually, I would like to determine the lowest omitted value from this ascending sorted list of numbers. I alreay found a JS function that would do the job by passing an integer array - and it works perfectly as desired.

Maybe we can somehow integrate the above mentioned requirement with the JS function below to determine the lowest omitted value.

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;

So, in a nutshell, I would like to determine the lowest omitted value from an array of strings.

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

Consequently, the result of the JS function should return 35 for the example stated above.

Thank you very much for your suggestions! I am looking forward to reading your comments.

4 Answers

You can use this simple regex replacement:

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

RegEx Demo

RegEx Details:

  • ^: Start
  • [a-zA-Z]+: Match an alphabet
  • 0*: Match zero or more 0s
  • Replacement is just an empty string

You could also use a single character class ^[A-Z0]+ to match 1 or more occurrences of A-Z or a 0 from the start of the string ^ and then use array map.

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

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

You can also use match instead of removing the unwanted characters.

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

RegEx Details:

  • [1-9]: the first character should be non zero number
  • [0-9]*: all the following numbers

The final function to get the lowest of all should look something like this.

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))

For the provided list sample a regular expression like ... (/^[0\D]+/) ... already is sufficient enough in order to be used for string replacement. Since this task, and also the additional task of casting a string into a number value, has to be processed for every array item, one wants to use Array.prototype.map.

In order to achieve the final result wished by the OP, one has to implement a function which gets the lowest omitted value from an unordered list of integers ...

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

Related