Ad

You have to create an abbreviation function. It will take string and return a string while taking the first and last char of word and replace the remaining with the number of chars in it.

For example:
internationalization => i18n
Localization => L10n
Deepak-Vishwa => D4k-V4a
I Love India => I L2e I3a

Code
Diff
  • const abbrev = (text = "") => {
      return text.replace(/\w{4,}/g, s => {
        l = s.length;
        return `${s[0]}${l - 2}${s[l - 1]}`;
      });
    };
    
    • const abbrevWord = (text = "") => {
    • if (text.length < 3) return text;
    • const first = text.charAt(0);
    • const last = text.slice(-1);
    • const remLen = text.length - 2;
    • return `${first}${remLen}${last}`;
    • };
    • const simpleChars = /\w+/g;
    • const abbrev = (text = "") => {
    • return text.replace(simpleChars, (matched) => abbrevWord(matched));
    • return text.replace(/\w{4,}/g, s => {
    • l = s.length;
    • return `${s[0]}${l - 2}${s[l - 1]}`;
    • });
    • };