Javascript var.replace 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4775206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:01:59  来源:igfitidea点击:

var.replace is not a function

javascript

提问by Brett

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.

我解决了这个问题......抱歉,我应该把代码放在我如何调用它上......意识到我不小心传递了表单字段本身的对象而不是它的值。

回答by ClosureCowboy

My guess is that the code that's calling your trimfunction is notactually passing a string to it.

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

To fix this, you can make stra string, like this: str.toString().replace(...)
...as alperpointed out below.

要解决此问题,您可以创建str一个字符串,如下所示:str.toString().replace(...)
...正如alper在下面指出的那样。

回答by T.Todua

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;
  • 变量是NUMBER(而不是字符串);
    num=35; num.replace(3,'three'); =====> ERROR
    num=35; num.toString().replace(3,'three'); =====> CORRECT !!!!!!
    num='35'; num.replace(3,'three'); =====> CORRECT !!!!!!
  • 变量是对象(而不是字符串);
  • 变量未定义;

回答by Kareem

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

如果您只想修剪字符串。您可以简单地使用“str.trim()”

回答by meder omuraliev

You are not passing a string otherwise it would have a replacemethod. I hope you didnt type function trim(str) { return var.replace(blah); }instead of return str.replace.

您没有传递字符串,否则它会有一个replace方法。我希望你没有输入function trim(str) { return var.replace(blah); }而不是return str.replace.

回答by gion_13

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

回答by Dave Vogt

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:

但是,如果你用非字符串的东西调用你的functoin,你确实会得到上面的错误:

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

回答by J.C. Gras

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

如果是数字,您可以尝试转换为字符串:

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

回答by user6619380

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

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

回答by Brett

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

无论如何,感谢您的回复。:)