Replacing the last character in a string javascript -
how replace last character of string?
setcookie('pre_checkbox', "111111111111 11 ") checkbox_data1 = getcookie('pre_checkbox'); if(checkbox_data1[checkbox_data1.length-1]==" "){ checkbox_data1[checkbox_data1.length-1]= '1'; console.log(checkbox_data1+"after"); } out put on console : 111111111111 11 after
last character not replaced '1' dont know why
also tried : checkbox_data1=checkbox_data1.replace(checkbox_data1.charat(checkbox_data1.length-1), "1");
could 1 pls me out
simple regex replace should want:
checkbox_data1 = checkbox_data1.replace(/.$/,1);
generic version:
mystr = mystr.replace(/.$/,"replacement");
remember calling str.replace()
doesn't apply change str
unless str = str.replace()
- is, apply replace()
function's return value variable str
Comments
Post a Comment