Javascript相当于PHP Explode()

Dou*_*eux 339 javascript php string

我有一个看起来像这样的字符串:

0000000020C90037:TEMP:数据

我需要在第一次冒号之后抓住所有内容,以便我有TEMP:数据.

我不经常使用Javascript,如果它是PHP我会这样做:

$str = '0000000020C90037:TEMP:data';
$arr = explode(":", $str);
$var = $arr[1].":".$arr[2];
Run Code Online (Sandbox Code Playgroud)

Joh*_*ock 596

这是从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'
Run Code Online (Sandbox Code Playgroud)

  • 应该注意的是,数组从[0]开始 (72认同)
  • @Herr Kaleun ......理解......但是OP想要阵列中的最后两个项目. (51认同)
  • *注*:`split(delimiter,limit)`的limit参数与`explode($ delimiter,$ string,$ limit)`的limit参数不同.例如:`explode('.','1.2.3.4',3)=== array('1','2','3.4')` - 在Javascript中,你会得到:`'1.2.3.4 '.split('.',3)=== ['1','2','3']`.有谁知道如何轻松复制PHP的方法? (12认同)
  • +1:那是对的; 这是来自MDN的链接:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split.和"相反"的方向,所以PHP implode()等效是myArray.join(':'):https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/join (4认同)

Fel*_*ing 44

你不需要拆分.你可以使用indexOfsubstr:

str = str.substr(str.indexOf(':')+1);
Run Code Online (Sandbox Code Playgroud)

但是,相当于explodesplit.

  • 有人可能认为你很讽刺.有时你必须解释一切.例如,"你的问题最好通过'indexOf'来解决......但'拆分'会逐字回答你的问题." (5认同)

psy*_*brm 33

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;
};
Run Code Online (Sandbox Code Playgroud)

应该完全模仿PHP的explode()函数.

'a'.explode('.', 2); // ['a']
'a.b'.explode('.', 2); // ['a', 'b']
'a.b.c'.explode('.', 2); // ['a', 'b.c']
Run Code Online (Sandbox Code Playgroud)

  • 根据我的意见,我只能提供**实际**等同于PHP的爆炸功能(根据原始问题). (6认同)
  • 多么简单而优雅的解决方案,我希望这是被接受的回应。 (2认同)

Que*_*tin 27

看起来你想分开


wel*_*rat 8

试试这个:

arr = str.split (":");
Run Code Online (Sandbox Code Playgroud)


JM *_*ign 6

创建一个对象:

// create a data object to store the information below.
    var data   = new Object();
// this could be a suffix of a url string. 
    var string = "?id=5&first=John&last=Doe";
// this will now loop through the string and pull out key value pairs seperated 
// by the & character as a combined string, in addition it passes up the ? mark
    var pairs = string.substring(string.indexOf('?')+1).split('&');
    for(var key in pairs)
    {
        var value = pairs[key].split("=");
        data[value[0]] = value[1];
    }

// creates this object 
    var data = {"id":"5", "first":"John", "last":"Doe"};

// you can then access the data like this
    data.id    = "5";
    data.first = "John";
    data.last  = "Doe";
Run Code Online (Sandbox Code Playgroud)

  • 似乎有点矫枉过正,反应为ty (7认同)

Ger*_*umm 5

使用 String.split

"0000000020C90037:TEMP:data".split(':')


sv8*_*rik 5

如果你喜欢 php,请看一下php.JS - JavaScriptexplode

或者在正常的 JavaScript 功能中:`

var vInputString = "0000000020C90037:TEMP:data";
var vArray = vInputString.split(":");
var vRes = vArray[1] + ":" + vArray[2]; `
Run Code Online (Sandbox Code Playgroud)