Sometimes, we want to replace the last character of string using JavaScript.
In this article, we’ll look at how to replace the last character of string using JavaScript.
How to replace the last character of string using JavaScript?
To replace the last character of string using JavaScript, we can use the string replace
method.
For instance, we write
const str1 = "Notion,Data,Identity,".replace(/.$/, ".");
to get the last character with the /.$/
.
$
matches the end of a word.
And we replace it with a '.'
with replace
.
Conclusion
To replace the last character of string using JavaScript, we can use the string replace
method.