8

我在 jQuery 中有字符串。

var s = "Text1, Text2, Text,true";

我需要将此变量分成两部分:

var s1 = substring(...); //after this s1="Text1, Text2, Text"
var s2 = substring(...); //after this s2="true"

你能帮我拆分变量吗?

4

5 回答 5

13
var s="Text1, Text2, Text,true";
var lastIndex = s.lastIndexOf(",")

var s1 = s.substring(0, lastIndex); //after this s1="Text1, Text2, Text"
var s2 = s.substring(lastIndex + 1); //after this s2="true"
于 2013-08-15T08:41:33.693 回答
3
s1 = s.split(',');
s2 = s1.pop();
s1 = s1.join(',');

(未测试)

文档

于 2013-08-15T08:41:28.697 回答
2
var index = s.lastIndexOf(",");
var first = str.substr(0, index );
var last = str.substr(index + 1);

或使用slice

var first = str.slice(0, index);
var last = str.slice(index +1);
于 2013-08-15T08:41:31.807 回答
1

尝试

var array = s.match(/(.*),(.*)/);
var s1 = array[1];
var s2 = array[2]
于 2013-08-15T08:40:46.347 回答
0
var s = "Text1, Text2, Text,true";
var n = s.lastIndexOf(",");

var s1 = s.substring(0, n);
var s2 = s.substring(n + 1);

http://jsfiddle.net/Y6yjr/

于 2013-08-15T08:48:19.727 回答