依次查找特殊标记,并对介于两者之间的文本进行处理 [英] Find special markers in sequence and do something to the text in between
问题描述
我想做一些类似于stackexchange中的短代码的工作.
I wanna do something that would work much like this shortcode in stackexchange.
因此,基本上,我想用不同的标记来包装文本,并相应地用特定类在跨度中.wrap()
将它们包装起来...同时还要删除曾经存在的标记.
So basically I want to wrap text with distinct markers and .wrap()
them in spans with specific classes accordingly... while also removing the markers that once existed.
我发现了这个使用jQuery在2个引号之间查找文本,但是它几乎没有帮助,因为我只能让它按原样工作.
I found this Find Text Between 2 Quotes with jQuery but it gives little help as i could only make it work as it was.
这进一步解释了: http://jsfiddle.net/ALfsT/3/
我不知道该去哪里.
推荐答案
在此感谢@Guffa
Thanks @Guffa for the help here
http://jsfiddle.net/mplungjan/AkCED/
var res = {
boldIt:/\*\*(.*?)\*\*/g,
underlineIt:/\_\_(.*?)\_\_/g
}
var txt = $( "#texts" ).html();
$.each(res, function(type, re) {
txt = txt.replace( re, '<span class="'+type+'" >$1</span>' );
});
$( "#texts" ).html(txt);
更新:
update:
现在我们需要编写如下代码: http://jsfiddle.net/mplungjan/bhTAM/
now we need to code stuff like this http://jsfiddle.net/mplungjan/bhTAM/
您将其更改为class = texts,将其更改为id = texts,效果更好
You changed to class=texts I changed it back to id=texts and it worked better
var res = {
boldIt:{re:/\*\*(.*?)\*\*/g,tag:"span"},
underlineIt:{re:/\_\_(.*?)\_\_/g,tag:"span"},
italicIt:{re:/\/\/(.*?)\/\//g,tag:"span"},
titleIt:{re:/\=\=(.*?)\=\=/g,tag:"h1"},
linkIt:{re:/\#\#(.*?)\:(.*?)\#\#/g, tag:"a"},
imageIt:{re:/\"\"(.*?)\:(.*?)\"\"/g, tag:"img"}
}
var s = $("#texts").html();
$.each(res, function(type, obj) {
if(s) s = s.replace(obj.re,'<'+obj.tag+' class="'+type+'" >$1</'+obj.tag+'>');
});
$("#texts").html(s);
这篇关于依次查找特殊标记,并对介于两者之间的文本进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!