i want to replace last input character from keyboard to ”
My String Input are
sample string
"<p><strong>abscd sample text</strong></p>"
"<p>abscd sample text!</p>"
My last character is dynamic that can be any thing between
a to z, A to Z, 0 to 9, any special characters([~ / < > & ( . ] ).
So i need to replace just that character
for example in Sample 1 i need to replace “t” and in sample 2 in need to replace “!”
I tried below code. but it id not worked for me
var replace = '/'+somechar+'$/';
Any way to do it?
Step one
to replace the a character in a string, use
replace()function of javaScript. Here is the MDN specification:Step two
you need to location the character to be replaced through regular expression. You want to replace the last character of a string and this could be expressed as /(.+)(.)$/.
.stands for any character,+means more than one character. Here(.+)matches all the character before the last one.(.)matches the last character.What you want to replace is the one inside the second brackets. Thus you use the same string matched in the first bracket with
$1and replace whatever after it.Here is the code to realize your intention: