I am looking for the correct syntax to remove all characters which immediately follow on from the final capital letter in a string. For example, 'FirstName' becomes 'FirstN'.
Can anyone help me with the JavaScript code?
I am looking for the correct syntax to remove all characters which immediately follow on from the final capital letter in a string. For example, 'FirstName' becomes 'FirstN'.
Can anyone help me with the JavaScript code?
Loop from the end to the beginning until you find a uppercase letter, then do a substring to keep only the part before. Here is an exemple on how to find if a letter is upper or not:
How can I test if a letter in a string is uppercase or lowercase using JavaScript?
function shorten(str) {
return str.slice(0, (function() {
for (var i=str.length; i--;) {
if (str.charAt(i).match(/[A-Z]/)) return ++i;
}
})())
}
You can use regex...
var text = "FirstName";
var newText = text.replace(/([A-Z])[a-z]*$/,"$1");