Javascript相当于PHP Explode()

本文翻译自:Javascript Equivalent to PHP Explode()

I have a string that looks like this: 我有一个看起来像这样的字符串:

0000000020C90037:TEMP:data 0000000020C90037:TEMP:数据

And I need to grab everything after the first colon, so that I have TEMP:data. 我需要在第一次冒号之后抓住所有内容,以便我有TEMP:数据。

I don't often work in Javascript, if it were PHP I would do this: 我不经常使用Javascript,如果它是PHP我会这样做:

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

#1楼

参考:https://stackoom.com/question/IwNf/Javascript相当于PHP-Explode


#2楼

With no intentions to critique John Hartsock , just in case the number of delimiters may vary for anyone using the given code, I would formally suggest to use this instead... 没有意图批评John Hartsock ,以防万一使用给定代码的任何人的分隔符数量不同,我会正式建议使用它代替......

var mystr = '0000000020C90037:TEMP:data';
var myarr = mystr.split(":");
var arrlen = myarr.length;
var myvar = myarr[arrlen-2] + ":" + myarr[arrlen-1];

#3楼

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;
};

Should mimic PHP's explode() function exactly. 应该完全模仿PHP的explode()函数。

'a'.explode('.', 2); // ['a']
'a.b'.explode('.', 2); // ['a', 'b']
'a.b.c'.explode('.', 2); // ['a', 'b.c']

#4楼

console.log(('0000000020C90037:TEMP:data').split(":").slice(1).join(':'))

outputs: TEMP:data 输出: TEMP:data

  • .split() will disassemble a string into parts .split()会将字符串反汇编成部分
  • .join() reassembles the array back to a string .join()将数组重新组装回字符串
  • when you want the array without it's first item, use .slice(1) 当你想要没有它的第一个项目的数组时,使用.slice(1)

#5楼

So I know that this post is pretty old, but I figured I may as well add a function that has helped me over the years. 所以我知道这篇文章很老了,但我想我也可以添加一个多年来帮助我的功能。 Why not just remake the explode function using split as mentioned above? 为什么不使用如上所述的拆分重制爆炸功能呢? Well here it is: 那么这里是:

function explode(str,begin,end)
{
   t=str.split(begin);
   t=t[1].split(end);
   return t[0];
}

This function works well if you are trying to get the values between two values. 如果您尝试获取两个值之间的值,此函数很有效。 For instance: 例如:

data='[value]insertdataherethatyouwanttoget[/value]';

If you were interested in getting the information from between the two [values] "tags", you could use the function like the following. 如果您有兴趣从两个[values]“tags”之间获取信息,可以使用如下所示的函数。

out=explode(data,'[value]','[/value]');
//Variable out would display the string: insertdataherethatyouwanttoget

But let's say you don't have those handy "tags" like the example above displayed. 但是,假设您没有像上面显示的示例那样方便的“标签”。 No matter. 不管。

out=explode(data,'insert','wanttoget');
//Now out would display the string: dataherethatyou

Wana see it in action? Wana看到它在行动? Click here . 点击这里


#6楼

var str = '0000000020C90037:TEMP:data';    // str = "0000000020C90037:TEMP:data"
str = str.replace(/^[^:]+:/, "");          // str = "TEMP:data"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值