简体   繁体   English

String.replace(Regex)返回未定义

[英]String.replace(Regex) returns undefineds

My example is paired down but I believe it communicates my problem: 我的示例已配对,但我相信它可以传达我的问题:

I have a simple string var data = '360–400 ' 我有一个简单的字符串var data = '360–400 ' for sake of example: 为了举例:

data.replace(' ');

returns: 收益:

//360–400undefined

Question: how do I make sure my output doesnt leave undefines in the string 问题:如何确保输出不会在字符串中留下未定义的内容

You need to pass a second parameter to the .replace() method in order to replace the matched value. 您需要将第二个参数传递给.replace()方法 ,以替换匹配的值。 If you simply want to remove the value, just pass an empty sting, '' : 如果您只想删除该值,只需传递一个空字符串''

data.replace(' ', '');

The reason it was replacing the value with undefined was simply because the second parameter was undefined . 之所以用undefined替换值是因为第二个参数是undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM