How to Capitalize Abbreviations and each first word in string?
<p>I am trying to create a function which should capitalize each first word in the sentence + it should also capitalize the abbreviated characters.</p>

<p>Example:<br>
a.m.a. archives of general psychiatry -> A.M.A. Archives of General Psychiatry</p>

<p>a.m.a. archives of neurology -> A.M.A. Archives of Neurology</p>

<p>a.m.a. archives of neurology and psychiatry -> A.M.A. Archives of Neurology and Psychiatry</p>

<p>Here is whats I have tried so far:</p>

<p>But no luck so far. </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>function transform(str) {
  let smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
  return str.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\'\s-]*/g, function(match, index, title) {
    if (index &gt; 0 &amp;&amp; index + match.length !== title.length &amp;&amp;
      match.search(smallWords) &gt; -1 &amp;&amp; title.charAt(index - 2) !== ":" &amp;&amp;
      (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &amp;&amp;
      (title.charAt(index + match.length) !== "'" || title.charAt(index - 1) === "'") &amp;&amp;
      title.charAt(index - 1).search(/[^\s-]/) &lt; 0) {
      return match.toLowerCase();
    }
    if (match.substr(1).search(/[A-Z]|\../) &gt; -1) {
      return match;
    }
    return match.charAt(0).toUpperCase() + match.substr(1);
  });
}

function showRes(str) {
  document.write(transform(str));
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;button onclick="showRes('a.m.a. archives of neurology')"&gt;convert&lt;/button&gt;</code></pre>
</div>
</div>
</p>