Максим Кадров Максим Кадров - 1 month ago 11
Javascript Question

Cut string from last capital letter till the end

I have a string variable, something like "thisMagazineRoomcode". I need slice this from "R" - last capital letter till the end of the string. Slice method doesn't suit me because I have different variable like "thisMagazineName" or "thisMagazineSquare" with different letter quantity after captial letter.

Answer

You can use String#replace method.

console.log(
  'thisMagazineRoomcode'.replace(/[A-Z][^A-Z]*$/, '')
);