这是我的文本输入值:
[quote Name Lastname]
text text text text text
[quote Name2 Lastname2]
text text text text text
[end]
[end]
我需要在->
中对其进行转换<blockquote>
<span> Name Lastname </span>
text text text text text
<blockquote>
<span> Name Lastname </span>
text text text text text
</blockquote>
</blockquote>
并做一些封闭,以防作者没有这样做。
这是我的实际源代码:
var oldMessage = $('#message').html();
var begin = /\[?quote\s[A-Za-z0-9\s]{1,}]/g;
var end = /\[?end]/g;
var beginMatch = oldMessage.match(begin);
var endMatch = oldMessage.match(end);
var beginMatchCounter = 0;
var endMatchCounter = 0;
if(beginMatch) {beginMatchCounter = oldMessage.match(begin).length;}
if(endMatch) {endMatchCounter = oldMessage.match(end).length}
var missingBegin = endMatchCounter - beginMatchCounter;
var missingEnd = beginMatchCounter - endMatchCounter;
var newMessage = '';
newMessage = newMessage + oldMessage.replace(begin,"<blockqoute>");
newMessage = newMessage.replace(end,"</blockqoute>");
for(var i = 0; i < missingBegin; i++) { newMessage = "<blockqoute>" + newMessage; }
for(var i = 0; i < missingEnd; i++) { newMessage = newMessage + "</blockqoute>"; }
$('#message').html(newMessage);
我知道我需要使用:
slice(7, txt.length-1)
但是不知道该怎么做,我应该使用forEach吗?