写了一些代码,以帮助Find special markers in sequence and do something to the text in between正则表达式工作不正常
这里是小提琴:http://jsfiddle.net/mplungjan/GyPHH/
为什么它只是每个正则表达式运行一次,你会如何处理嵌套项
<div id="texts">
**this** **would** make it **bold** __this would__ make it __underlined__
__**how about bold and underlined**__
and
**__the other way around__**
</div>
var res = {
boldIt:/\*\*(.*?)\*\*/g,
underlineIt:/\_\_(.*?)\_\_/g
}
$.each(res, function(type, re) {
var s = $("#texts").html();
var m = re.exec(s);
var found = m[0];
$.each(m, function(index, value) {
if (index==0) return true;
var html = s.replace(found,'<span class="'+type+'" >'+value+'</span>',"g");
$("#texts").html(html);
});
});
像这样的嵌套问题是* irregular *上下文无关语法的教科书示例。正则表达式不会可靠地解析它们,您需要一个FSM。不,不是[那个](http://en.wikipedia.org/wiki/Flying_Spaghetti_Monster),[那个](http://en.wikipedia.org/wiki/Finite_state_machine)。 – Malvolio
感谢@Malvolio – mplungjan