Categories
JavaScript Answers

How to replace the last character of string using JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *