PHP Explode() に相当する Javascript 質問する

PHP Explode() に相当する Javascript 質問する

次のような文字列があります:

0000000020C90037:TEMP:データ

この文字列が必要です:

TEMP:データ。

PHP では次のようにします:

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

explodePHP と同じように JavaScript で文字列を効果的に処理するにはどうすればよいですか?

ベストアンサー1

これは 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'

おすすめ記事