s="[b]foo[/b]foo[s]foo[/s]" re=/(\[)|(\])/g alert(s.replace(re, function(a, b, c){if(a===b) return "<"; if(a===c) return">"})) // <b>foo</b>foo<s>foo</s>