简体   繁体   中英

separate first character from string

I am trying to separate the number which is the first character of a string from the rest of the string. For example 1options to select should be 1 options to select.

Please note that the select list is dynamically generated

<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.                      
});

I'd do it in the following way:

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

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

You can do:

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

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