如何使用特定字符拆分字符串?

时间:2014-03-05 19:42:39

标签: jquery

<div id='test'>abc+defg+323+gh2+ab3</div>

JS

var a = $('#test').html();

以下是否可能:

var b = content of a before the first plus sign
var c = content of a between the first and the second plus sign
var d = content of a between the second and the third plus sign
var e = content of a between the third and the fourth plus sign

加号始终是唯一的,即它仅用作分隔符,而不是字符串本身。

2 个答案:

答案 0 :(得分:2)

试试这个:

var vals = a.split('+'); //yields an array of whats between the +'s

var b = vals[0], c=vals[1], d=vals[2], e=vals[2];

:)

答案 1 :(得分:0)

试试这个:

var a = $('#test').html();
var array = a.split('+');
var b= array[0];
var c= array[1]; ...