2009-12-01 25 views
1

我有一个变量的字符串:比较和JavaScript函数内部字符串的分裂

var test= "http://[email protected]%@http://[email protected]%@http://[email protected]%@"; 

我想这个字符串的特殊字符的出现分裂,即:@%@,然后拆分后,我想推动这件事情是这样的数组:

var spcds = []; 
    spcds.push("http://www.gmail.com"); 
spcds.push("http://www.google.com"); 
spcds.push("http://www.yahoo.com"); 

我需要的仅仅是分割字符串变量,即推到spcds阵列。我怎样才能在我的JavaScript函数中做到这一点,使得结果值将被存储到另一个变量,然后我将其推送到数组spcds

回答

5

使用split method分割字符串:

var parts = test.split("@%@"); 

如果你不想有空部分,使用filter method过滤掉不是空字符串的值:

var parts = test.split("@%@").filter(function(val){return val!="";}); 
1

这会给你一个你之后的字符串数组然后你可以根据需要迭代数组。

var mySplitResult = myString.split("@%@");