How to split a string at the first `/` (slash) and surround part of it in a ``?

前端 未结 7 1739
独厮守ぢ
独厮守ぢ 2020-11-28 01:28

I want to format this date:

23/05/2013
.

First I want to split the string at the first / and have the res

7条回答
  •  自闭症患者
    2020-11-28 02:13

    Instead of using substring with a fixed index, you'd better use replace :

    $("#date").html(function(t){
        return t.replace(/^([^\/]*\/)/, '$1
    ') });

    One advantage is that it would still work if the first / is at a different position.

    Another advantage of this construct is that it would be extensible to more than one elements, for example to all those implementing a class, just by changing the selector.

    Demonstration (note that I had to select jQuery in the menu in the left part of jsfiddle's window)

提交回复
热议问题