替换不是一个函数。

时间:2022-07-22 22:32:46

I'm using the below code to try to trim the string in Javascript but am getting the error mentioned in the title:

我正在使用下面的代码来尝试在Javascript中修改字符串,但是在标题中出现了错误:

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.

我固定问题....对不起,我应该把代码如何我也被称之为....意识到我无意中传递了表单字段本身的对象,而不是它的值。

9 个解决方案

#1


93  

My guess is that the code that's calling your trim function is not actually passing a string to it.

我的猜测是,调用您的trim函数的代码实际上并没有传递一个字符串给它。

If you post that code, I could update my answer.

如果你发布这段代码,我可以更新我的答案。

#2


61  

probable issues:

  • variable is not defined;
  • 变量没有定义;
  • 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 !!!!!!
  • 变量是数字(而不是字符串);num = 35;num.replace(3,' 3 ');= = = = = >错误num = 35;num.toString().replace(3 ' 3 ');= = = = = >正确! ! ! ! ! !num =“35”;num.replace(3,' 3 ');= = = = = >正确! ! ! ! ! !
  • variable is object (instead of string);
  • 变量是对象(而不是字符串);

#3


7  

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

如果你只想修剪这根线。您可以简单地使用“.trim()”

#4


5  

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

#5


4  

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.

您没有传递一个字符串,否则它将有一个替换方法。我希望您没有键入函数trim(str) {return var.replace(blah);代替return .replace。

#6


4  

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

你的功能正常吗?Ie。你传递的东西是否真的是一个字符串?

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

#7


4  

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

如果有一个数字,你可以尝试转换为字符串:

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

#8


2  

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

您应该使用java脚本的toString()方法来转换成字符串,因为替换方法是一个字符串函数。

#9


0  

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

谢谢你的回复。:)

#1


93  

My guess is that the code that's calling your trim function is not actually passing a string to it.

我的猜测是,调用您的trim函数的代码实际上并没有传递一个字符串给它。

If you post that code, I could update my answer.

如果你发布这段代码,我可以更新我的答案。

#2


61  

probable issues:

  • variable is not defined;
  • 变量没有定义;
  • 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 !!!!!!
  • 变量是数字(而不是字符串);num = 35;num.replace(3,' 3 ');= = = = = >错误num = 35;num.toString().replace(3 ' 3 ');= = = = = >正确! ! ! ! ! !num =“35”;num.replace(3,' 3 ');= = = = = >正确! ! ! ! ! !
  • variable is object (instead of string);
  • 变量是对象(而不是字符串);

#3


7  

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

如果你只想修剪这根线。您可以简单地使用“.trim()”

#4


5  

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

#5


4  

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.

您没有传递一个字符串,否则它将有一个替换方法。我希望您没有键入函数trim(str) {return var.replace(blah);代替return .replace。

#6


4  

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

你的功能正常吗?Ie。你传递的东西是否真的是一个字符串?

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

#7


4  

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

如果有一个数字,你可以尝试转换为字符串:

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

#8


2  

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

您应该使用java脚本的toString()方法来转换成字符串,因为替换方法是一个字符串函数。

#9


0  

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

谢谢你的回复。:)