3 回答

TA貢獻1784條經(jīng)驗 獲得超9個贊
這是您的PHP代碼的直接轉換:
//Loading the variable
var mystr = '0000000020C90037:TEMP:data';
//Splitting it with : as the separator
var myarr = mystr.split(":");
//Then read the values from the array where 0 is the first
//Since we skipped the first element in the array, we start at 1
var myvar = myarr[1] + ":" + myarr[2];
// Show the resulting value
console.log(myvar);
// 'TEMP:data'

TA貢獻1828條經(jīng)驗 獲得超13個贊
您無需拆分。您可以使用indexOf和substr:
str = str.substr(str.indexOf(':')+1);
但是,相當于explode將split。

TA貢獻1825條經(jīng)驗 獲得超4個贊
String.prototype.explode = function (separator, limit)
{
const array = this.split(separator);
if (limit !== undefined && array.length >= limit)
{
array.push(array.splice(limit - 1).join(separator));
}
return array;
};
應該準確地模仿PHP的explode()函數(shù)。
'a'.explode('.', 2); // ['a']
'a.b'.explode('.', 2); // ['a', 'b']
'a.b.c'.explode('.', 2); // ['a', 'b.c']
添加回答
舉報