Remove Nested Patterns with One Line of JavaScript
Popularity Report
![]() |
|||
![]() |
|||
![]() |
|||
![]() |
|||
![]() |
|||
![]() |
URL Tag Cloud
Bookmark History
Saved by 2 people (0 private), first by anonymouse user on 2008-05-30
- Samgoody on 2009-04-06 - Tags Scripting , Regex , Snippets , stevenleviathon
- Bluecockatoo on 2008-05-30 - Tags javascript , regex , codesnippets , tutorials
Public Sticky notes
var str = "abc<1<2<>3>4>def";
while (str != (str = str.replace(/<[^<>]*>/g, "")));
// str -> "abcdef"
Highlighted by bluecockatoo
var str = "abc(d(e())f)(gh)ijk()",
re = /\([^()]*\)/,
output = [],
match, parts, last;
while (match = re.exec(str)) {
parts = match[0].split("\uFFFF");
if (parts.length < 2)
last = output.push(match[0]) - 1;
else
output[last] = parts[0] + output[last] + parts[1];
str = str.replace(re, "\uFFFF");
}
// output -> ["(d(e())f)", "(gh)", "()"]
Highlighted by bluecockatoo


Public Comment