2013-05-21 21 views
0

我想從字符串的其餘部分中分離出第一個字符的數字。 例如,要選擇的1選項應該是1個選項來選擇。與字符串分開的第一個字符

請注意,選擇列表是動態生成

<select> 
    <option>1options to select</option> 
    <option>2anything can be here</option> 
    <option>3anything can be here</option> 
    <option>5anything can be here</option> 
</select> 

$(".custom-ordered-list select option").each(function() { 

    //i think i need to loop through but not sure what to do next.      
}); 
+0

可以選9999選項嗎?或'9999東西'? –

+0

是的,它可以.. – gables20

回答

4

我會做下列方式:

$(".custom-ordered-list select option").text(function(i, text) { 
    return text.replace(/^\d+/, "$& "); 
}); 

DEMO:http://jsfiddle.net/63h7T/

+0

+1這是更清潔.. – techfoobar

+0

這完成了這項工作。 – gables20

+1

@roasted閱讀問題下方的評論。 – VisioN

1

你可以這樣做:

$(".custom-ordered-list select option").each(function() { 
    $(this).text($(this).text().replace(/^(\d+)(.+)$/, '$1 $2')); 
}); 
+0

這也很完美。 – gables20

相關問題