JavaScript split function

前端 未结 2 1226
情深已故
情深已故 2020-12-12 05:28

i like to split a string depending on \",\" character using JavaScript

example

var mystring=\"1=name1,2=name2,3=name3\";

need out

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 05:51

    Just use string.split() like this:

    var mystring="1=name1,2=name2,3=name3";
    var arr = mystring.split(','); //array of ["1=name1", "2=name2", "3=name3"]
    

    If you the want string version of result (unclear from your question), call .join() like this:

    var newstring = arr.join(' '); //(though replace would do it this example)
    

    Or loop though, etc:

    for(var i = 0; i < arr.length; i++) {
      alert(arr[i]);
    }
    

    You can play with it a bit here

提交回复
热议问题