我在 jQuery 中有字符串。
var s = "Text1, Text2, Text,true";
我需要将此变量分成两部分:
var s1 = substring(...); //after this s1="Text1, Text2, Text"
var s2 = substring(...); //after this s2="true"
你能帮我拆分变量吗?
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"
s1 = s.split(',');
s2 = s1.pop();
s1 = s1.join(',');
(未测试)
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);
尝试
var array = s.match(/(.*),(.*)/);
var s1 = array[1];
var s2 = array[2]
var s = "Text1, Text2, Text,true";
var n = s.lastIndexOf(",");
var s1 = s.substring(0, n);
var s2 = s.substring(n + 1);