一旦バラして、くつける、かな。
大変申し訳ございません。文字列は空白が無い状態からの置換をお伺いするべきでしたが、...
js
1var text = "aaaa <aa>(aaa)aaaaa";
2
3var result =
4 text
5 .replace(new RegExp("([<( ])", "g"), "\n$1")
6 .replace(new RegExp("([>) ])", "g"), "$1\n")
7 .split("\n")
8 .map(it => {
9 if (it.match(/^[<(][^<(]+[>)]$/)) return it;
10 else return it.replace(new RegExp("a", "g"), "B");
11 })
12 .join("")
13
14console.log(text);
15console.log(result);
16
17// aaaa <aa>(aaa)aaaaa
18// BBBB <aa>(aaa)BBBBB
~~ var text = "aaaa <aa> (aaa) aaaaa"; ~~
~~ ~~
~~ var result = ~~
~~ text.split(" ") ~~
~~ .map(it => { ~~
~~ if (it.match(/^[<(][^<(]+[>)]$/)) return it; ~~
~~ else return it.replace(new RegExp("a", "g"), "B"); ~~
~~ }) ~~
~~ .join(" ") ~~
~~ ~~
~~ console.log(text); ~~
~~ console.log(result); ~~
~~ ~~
~~ // aaaa <aa> (aaa) aaaaa ~~
~~ // BBBB <aa> (aaa) BBBBB ~~