在下面的字符串中,我想用適當的下拉菜單替換[choice:a3d]。我不確定這些選項需要在冒號後面和方形大括號之前格式化。調用基於正則表達式匹配的javascript函數
string = "operation [number] [choice:a3d] [number]";
我不太確定.replace函數來自哪裏,但我正在使用的代碼已導入jquery。
string.replace(/(?:\[choice\:)(\w+)(?:\])/g, choice_func);
其中:
function choice_func(choice_lists, listname, default_opt)
{
console.log("choice_lists: "+choice_lists); // [choice:a3d]
console.log("listname: "+listname); // a3d
console.log("default_option: "+default_opt); // 67
var list = choice_lists[listname];
return '<span class="string ' + listname + ' autosocket"><select>' +
list.map(function(item)
{
if (item === default_opt){
return '<option selected>' + item + '</option>';
}else{
return '<option>' + item + '</option>';
}
}).join('') +'</select></span>';
}
不用說,代碼失敗,出現錯誤「遺漏的類型錯誤:無法調用‘地圖’的未定義」
也是在做參數的函數來自?
不要以爲該代碼是否是正確的....
的問題是有點混亂,你認爲你可以創建你想達到什麼樣的一個jsbin例子嗎? –
這有幫助嗎? http://jsbin.com/asisur/edit – TechplexEngineer
我建議你不要這樣寫代碼。在3年後回到此代碼時請記住我的建議。 –