我有以下代碼:分割字符串的特定字符
var string = "word1;word2;word3,word4,word5,word6.word7";
function ends_with(string, character) {
var regexp = new RegExp('\\w+' + character, 'g');
var matches = string.match(regexp);
var replacer = new RegExp(character + '$');
return matches.map(function(ee) {
return ee.replace(replacer, '');
});
}
// ends_with(string, ';') => ["word1", "word2"]
該函數沒有考慮空白。例如,如果輸入
ends_with('Jonas Sand,', ',')
的輸出將是Sand。需要關於使用帶有空格的單詞進行功能工作的幫助。
請把你的代碼放在代碼塊 - CTRL + K :) – 2009-12-10 14:16:43