Adam Coulombe
Adam Coulombe

Reputation: 1505

Remove capital letters from end of string

I have a bunch of strings that look like:

var uglystrings = ["ChipBagYAHSC","BlueToothNSJ"]

They all have a 2-5 capital letters at the end. I would like to remove the capital letters from the end using js but I am not sure what is the most efficient way? I cannot do substr because they all will have a different number of capital letters at the end

Upvotes: 4

Views: 133

Answers (1)

Ori Drori
Ori Drori

Reputation: 191986

Iterate the array using Array#map, and replace the uppercase letters of at the end of each string using a RegExp (regex101):

var uglystrings = ["ChipBagYAHSC","BlueToothNSJ"];

var result = uglystrings.map(function(str) {
  return str.replace(/[A-Z]+$/, '');
});

console.log(result);

Upvotes: 8

Related Questions