jQuery string split the string after the space using split() method

前端 未结 3 1118
眼角桃花
眼角桃花 2021-02-07 13:27

my code

  var str =$(this).attr(\'id\');

this will give me value == myid 5

   var str1 = myid
   var str2 = 5         


        
3条回答
  •  时光取名叫无心
    2021-02-07 13:43

    Use in-built function: split()

    var source = 'myid 5';
    
    //reduce multiple places to single space and then split
    var splittedSource = source.replace(/\s{2,}/g, ' ').split(' ');
    
    console.log(splittedSource);
    

    ​ Note: this works even there is multiple spaces between the string groups

    Fiddle: http://jsfiddle.net/QNSyr/6/

提交回复
热议问题