I\'m using the below code to try to trim the string in Javascript but am getting the error mentioned in the title:
function trim(str) {
return str.replac
I fixed the problem.... sorry I should have put the code on how I was calling it too.... realized I accidentally was passing the object of the form field itself rather than it's value.
Thanks for your responses anyway. :)
In case of a number you can try to convert to string:
var stringValue = str.toString();
return stringValue.replace(/^\s+|\s+$/g,'');
num=35; num.replace(3,'three'); =====> ERROR
num=35; num.toString().replace(3,'three'); =====> CORRECT !!!!!!
num='35'; num.replace(3,'three'); =====> CORRECT !!!!!!
You should probably do some validations before you actually execute your function :
function trim(str) {
if(typeof str !== 'string') {
throw new Error('only string parameter supported!');
}
return str.replace(/^\s+|\s+$/g,'');
}
make sure you are passing string to "replace" method. Had same issue and solved it by passing string. You can also make it to string using toString() method.
You are not passing a string otherwise it would have a replace
method. I hope you didnt type function trim(str) { return var.replace(blah); }
instead of return str.replace
.