0
재미있는 임시 변통 BBCode 파서를 만들려고합니다. 동일한 태그가 아닌 모든 태그를 대체 할 수 있지만 두 개 이상의 동일한 태그를 바꿀 수는 없습니다. . 예를 들어, 나는 다음과 같은 BB 있습니다중첩 된 교체 JS
[big][big][c red]WARNING[/c][/big][/big]
그것만이 출력 :
<span style="font-size: larger;">[big]<span style="color: red">WARNING</span></span>[/big]
다음과 같이 내가 사용하고있는 코드는 다음과 같습니다
function parseBBCode(text, element) {
//RegExp for BBCode to find
var find = new Array();
find[0] = /\[b\](.*?)\[\/b\]/g; //Bold
find[1] = /\[bg (.*?)\](.*?)\[\/bg\]/g; //Background
find[2] = /\[big\](.*?)\[\/big\]/g; //Big
find[3] = /\[c (.*?)\](.*?)\[\/c\]/g; //Color
find[4] = /\[f (.*?)\](.*?)\[\/f\]/g; //Font
find[5] = /\[i\](.*?)\[\/i\]/g; //Italics
find[6] = /\[img (.*?)\]/g; //Image
find[7] = /\[p\](.*?)\[\/p\]/g; //Pre
find[8] = /\[small\](.*?)\[\/small\]/g; //Small
find[9] = /\[s\](.*?)\[\/s\]/g; //Strike
var replace = new Array();
replace[0] = "<span style=\"font-weight: bold;\">$1</span>";
replace[1] = "<span style\"background-color: $1\">$2</span>";
replace[2] = "<span style=\"font-size: larger;\">$1</span>";
replace[3] = "<span style=\"color: $1\">$2</span>";
replace[4] = "<span style=\"font-family: $1\">$2</span>";
replace[5] = "<span style=\"font-style: italic;\">$1</span>";
replace[6] = "<a href=\"$1\"><img src=\"$1\" style=\"max-height: 200px; max-width: 200px;\" /></a>";
replace[7] = "<pre>$1</pre>";
replace[8] = "<small>$1</small>";
replace[9] = "<span style=\"text-decoration: line-through;\">$1</span>";
for (var i = 0; i < find.length; i++) {
text = text.replace(find[i], replace[i]);
}
$(element).html(text);
}
내가이 만들 수 어쨌든 있나요 중첩 된 요소도 교체 하시겠습니까?