简体   繁体   中英

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 ' 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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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