Javascript Regex: how to remove string before > and including >
I have a string like so
开发者_如何学JAVAitem[3]>something>another>more[1]>here
hey>this>is>something>new
.
.
.
I would like to produce the following for each iteration indicated by each new line
item[3]>something>another>more[1]>here
something>another>more[1]>here
another>more[1]>here
more[1]>here
here
Another example:
hey>this>is>something>new
this>is>something>new
is>something>new
something>new
new
I would like a regex or some way to incrementally remove the furthest left string up to >.
You could do it usingString.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));
}
See also: Array.shift()
.
Demo →
myString.replace(/^[^>]*>/, "")
To iterate through the cases, perhaps try this:
while (str.match(/^[^>]*>/)) {
str = str.replace(/^[^>]*>/, '');
// use str
}
精彩评论