Remove capital letters from end of line

I have a group of lines that look like this:

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

All of them have 2-5 capital letters at the end. I would like to remove capital letters from the end using js, but I'm not sure what is the most efficient way? I can't do substrit because everyone at the end will have a few capital letters

+4
source share
1 answer

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

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

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

console.log(result);
Run code
+8
source

Source: https://habr.com/ru/post/1691062/


All Articles