var.replace is not a function

Javascript

Javascript Problem Overview


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.replace(/^\s+|\s+$/g,'');
}

Edit:

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 its value.

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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;

Solution 3 - Javascript

> 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()"

Solution 4 - Javascript

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.

Solution 5 - Javascript

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,'');
}

Solution 6 - Javascript

Did you call your function properly? Ie. is the thing you pass as as a parameter really a string?

Otherwise, I don't see a problem with your code - the example below works as expected

function trim(str) {
    return str.replace(/^\s+|\s+$/g,'');
}


trim('    hello   ');  // --> 'hello'

However, if you call your functoin with something non-string, you will indeed get the error above:

trim({});  // --> TypeError: str.replace is not a function

Solution 7 - Javascript

In case of a number you can try to convert to string:

var stringValue = str.toString();
return stringValue.replace(/^\s+|\s+$/g,'');

Solution 8 - Javascript

You should use toString() Method of java script for the convert into string before because replace method is a string function.

Solution 9 - Javascript

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. :)

Solution 10 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrettView Question on Stackoverflow
Solution 1 - JavascriptClosureCowboyView Answer on Stackoverflow
Solution 2 - JavascriptT.ToduaView Answer on Stackoverflow
Solution 3 - JavascriptKareemView Answer on Stackoverflow
Solution 4 - Javascriptmeder omuralievView Answer on Stackoverflow
Solution 5 - Javascriptgion_13View Answer on Stackoverflow
Solution 6 - JavascriptDave VogtView Answer on Stackoverflow
Solution 7 - JavascriptJ.C. GrasView Answer on Stackoverflow
Solution 8 - Javascriptuser6619380View Answer on Stackoverflow
Solution 9 - JavascriptBrettView Answer on Stackoverflow
Solution 10 - Javascriptayaz-bin-mukhtarView Answer on Stackoverflow