我正在使用javascript split()
方法,但我想用其他任何方法覆盖逗号分隔符:
例如:
而不是这样:
var str = "How are you doing today?";
var res = str.split(" ");
//Result : How,are,you,doing,today?
我想有这个:
var str = "How are you doing today?";
var res = str.split(" ");
//Result : How%are%you%doing%today?
您对如何实现这一目标有任何想法吗? 谢谢!
答案 0 :(得分:0)
Split用于将String over delimiter拆分为Array作为示例:
var str = "How are you doing today?";
var res = str.split(" "); // ["How", "are", "you", "doing", "today?"]
你可以拆分其他任何东西,比如在逗号上拆分,你想要实现的是获取字符串并用%替换空格可以通过两种方式完成:
首先:在空间上拆分并单独获取每个单词的数组,然后在所需的分隔符上再次连接它们,因为在此示例中为逗号:
var str = "How are you doing today?";
var res = str.split(" ").join("%"); // "How%are%you%doing%today?"
第二:用逗号替换字符串中的每个空格,/g
g用于将所有出现的空格替换为第一个空格:
var str = "How are you doing today?";
var res = str.replace(/ /g,"%");; // "How%are%you%doing%today?"