Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove capital letters from end of string

Tags:

javascript

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

like image 946
Adam Coulombe Avatar asked Jan 30 '23 00:01

Adam Coulombe


1 Answers

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);
like image 82
Ori Drori Avatar answered Feb 02 '23 00:02

Ori Drori