var.replace is not a function
probable issues:
- variable is NUMBER (instead of string);
num=35; num.replace(3,'three'); =====> ERROR
num=35; num.toString().replace(3,'three'); =====> CORRECT !!!!!!
num='35'; num.replace(3,'three'); =====> CORRECT !!!!!!
- variable is object (instead of string);
- variable is not defined;
Replace wouldn't replace numbers. It replaces strings only.
This should work.
function trim(str) {
return str.toString().replace(/^\s+|\s+$/g,'');
}
If you only want to trim the string. You can simply use "str.trim()"
My guess is that the code that's calling your trim
function is not actually passing a string to it.
To fix this, you can make str
a string, like this: str.toString().replace(...)
...as alper pointed out below.