For examples
test1, test2, test3, test4,
How do I replace the very last character (comma) with a period?
This removes the trailing comma if any and adds a period:
textarea.value = textarea.value.replace(/,$/, "") + ".";
textarea.value
is a string which has a replace
method. As first argument, a regular expression is given (characterized by a single leading /
) which matches a comma on the end ($
). The match (if any) is replaced by nothing (removed) and a period is appended.
Beware that this code resets the scrolling (at least in Firefox) and the cursor position.
Another snippet that removed a traling comma, but which does not add a period if there is no trailing comma:
textarea.value = textarea.value.replace(/,$/, ".");
You can use .slice()
to remove the last character, then just concatenate the period.
var ta = document.getElementById('mytextarea');
ta.value = (ta.value.slice(0,-1) + '.');
var yourTextarea = document.getElementById('textareaId'); // get your textarea element
var val = yourTextarea.value; // get text, written in textarea
val = val.slice(0,-1); // remove last char
val += charToReplace; // add char, that you want to be placed instead of comma
yourTextarea.value = str; // set just edited text into textarea
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With