javascript 替换没有定义,但为什么呢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3901160/
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-10-25 02:36:58  来源:igfitidea点击:

Replace is not defined, but why?

javascript

提问by Peter


can somebody tell me what is wrong?


有人可以告诉我出了什么问题吗?

JS Code

JS代码

$.ajax({
    url:"http://www.google.com/complete/search?qu=chicken",
    success:function(data){

        var test_data = ''+data+''; // convert object to a string
        $('body').append(typeof(test_data));

        var test_data = replace.test_data(/[0-9]/,'X');
        $('body').append('<hr />'+test_data+' <hr />');

    },
    dataType:'jsonp',
    error:function(){
        alert('error');
    }
});

jsfiddlehttp://www.jsfiddle.net/V9Euk/664/

jsfiddle http://www.jsfiddle.net/V9Euk/664/

Thanks in advance!
Peter

提前致谢!
彼得

回答by Pointy

You got it backwards; it should be

你倒退了;它应该是

test_data.replace(...);

Also, you don't need varbefore the second assignment to "test_data"; just the first one.

此外,您不需要var在第二次分配给“test_data”之前;只是第一个。

回答by BrunoLM

replaceis undefined. It is not an object.

replace未定义。它不是一个对象。

Use replacefrom the string prototype

使用replace从字符串原型

"string".replace(//, "");

As you can see:

如你看到的:

alert(String.prototype.replace)

Results in

结果是

function replace() {
    [native code]
}

回答by Bang Dao

You should replace replace.test_databy test_data.replace

你应该替换replace.test_datatest_data.replace