gables20
gables20

Reputation: 496

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

Upvotes: 0

Views: 42

Answers (2)

techfoobar
techfoobar

Reputation: 66663

You can do:

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

Upvotes: 1

VisioN
VisioN

Reputation: 145408

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/

Upvotes: 4

Related Questions