Javascript相当于PHP Explode() [英] Javascript Equivalent to PHP Explode()

查看:149
本文介绍了Javascript相当于PHP Explode()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的字符串:

I have a string that looks like this:


0000000020C90037:TEMP:data

0000000020C90037:TEMP:data

我需要在第一次冒号之后抓住所有内容,这样我才能获得TEMP:数据。

And I need to grab everything after the first colon, so that I have TEMP:data.

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

I don't often work in Javascript, if it were PHP I would do this:

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


推荐答案

这是从PHP代码直接转换:

This is a direct conversion from your PHP code:

//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'

这篇关于Javascript相当于PHP Explode()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆