2011-01-12 78 views
1

我有一个像这样的Javascript正则表达式:如何前>和包括删除字符串>

item[3]>something>another>more[1]>here 
hey>this>is>something>new 
. 
. 
. 

串,我想产生由每个新行

item[3]>something>another>more[1]>here 
something>another>more[1]>here 
another>more[1]>here 
more[1]>here 
here 

表示每次迭代以下又如:

hey>this>is>something>new 
this>is>something>new 
is>something>new 
something>new 
new 

我想正则表达式或某种方式来逐步去除最左的字符串最多>

回答

2

你可以使用String.split()做到这一点:

var str = 'item[3]>something>another>more[1]>here', 
    delimiter = '>', 
    tokens = str.split(delimiter); // ['item[3]', 'something', 'another', 'more[1]', 'here'] 

// now you can shift() from tokens 
while (tokens.length) 
{ 
    tokens.shift(); 
    alert(tokens.join(delimiter)); 
} 

参见:Array.shift()

Demo →

1

要通过迭代的情况下,也许试试这个:

while (str.match(/^[^>]*>/)) { 
    str = str.replace(/^[^>]*>/, ''); 
    // use str 
} 
相关问题