Split string on the first white space occurrence

后端 未结 13 2015
孤街浪徒
孤街浪徒 2020-11-28 20:07

I didn\'t get an optimized regex that split me a String basing into the first white space occurrence:

var str=\"72 tocirah sneab\";

I need

13条回答
  •  春和景丽
    2020-11-28 20:42

    Whenever I need to get a class from a list of classes or a part of a class name or id, I always use split() then either get it specifically with the array index or, most often in my case, pop() to get the last element or shift() to get the first.

    This example gets the div's classes "gallery_148 ui-sortable" and returns the gallery id 148.

    var galleryClass = $(this).parent().prop("class"); // = gallery_148 ui-sortable
    var galleryID = galleryClass.split(" ").shift(); // = gallery_148
    galleryID = galleryID.split("_").pop(); // = 148
    //or
    galleryID = galleryID.substring(8); // = 148 also, but less versatile 
    

    I'm sure it could be compacted into less lines but I left it expanded for readability.

提交回复
热议问题