This removes the trailing comma, if any, and adds a period:
textarea.value = textarea.value.replace(/,$/, "") + ".";
textarea.valueis a string that has a method replace. The first argument is a regular expression (characterized by one leading /) that matches the comma at the end ( $). The match (if any) is replaced by nothing (is deleted) and a period is added.
Remember that this code resets the scroll (at least in Firefox) and the cursor position.
Another fragment that removes the trial comma, but which does not add a period if there is no trailing comma:
textarea.value = textarea.value.replace(/,$/, ".");