Shorten Javascript Function
javascript
Solution
An even shorter one:
str.replace(/(and|of|the|for|to)( |$)/gi, "").replace(/(.).+?(\s|$)/g, "$1.");
To make sure it is capitalized you can do `.toUpperCase` at the end.
(.) //selects the first character
.+ //matches the rest of the characters
? //? indicates a lazy match
(\s|$) //match a space or the end
$1. //means "the first selected match plus a dot"
Let's make it into one Regex!
str.replace(/((and|of|the|for|to) )*(.).+?(\s|$)/ig, "$3.");
"Royal Society for the Prevention of Cruelty to Animals"
.replace(/((and|of|the|for|to) )*(.).+?(\s|$)/ig, "$3.");
//R.S.P.C.A
"Josie and the Pussycats"
.replace(/((and|of|the|for|to) )*(.).+?(\s|$)/ig, "$3.");
//J.P.
This should, in theory, cover all legit names. For names with preposition(s) at the end, you can technically do this:
.replace(/((and|of|the|for|to) )*(.).+?(\s|$)((and|of|the|for|to) ?)*/ig, "$3.")
But this is clearly longer than the one with two `replace`s and this defeats its purpose.
Problem
I wrote myself a function to turn a string into an abbreviation and it's currently fairly long and it's case-sensitive. I need a way shorten it so it works 100% of the time. Currently it screws up if one of the splitting words has a capital, if a word ends in a splitting word. My splitting words are basically the words I'm removing (as most companies and such don't include them). They include: - and - of - the - for - to Also, the way I'm removing them is using split and join (`str.split('and ').join('')`) which to me doesn't seem like the easiest way. Other than these issues, it works fine. Could anyone help me shrink the function and fix the issues? Thanks. Function: ``` String.prototype.toAbbrev = function () { var s = []; var a = this.split('and ').join('').split('of ').join('').split('the').join('').split('for ').join('').split('to ').join('').split(' '); for (var i = 1; i < a.length + 1; i++) { s.push(a[i - 1].charAt(0).toUpperCase()); } return s.join('.'); } ``` Outputs on Tested Companies ``` The National Aeronautics and Space Administration -> N.A.S.A The National Roads and Motorists' Association -> N.R.M.A Royal Society for the Prevention of Cruelty to Animals -> R.S.P.C.A ```