Javascript 使用js正则表达式替换简单的标记样式,如**粗体**到<;b>;粗体</b>;
我试图获取一块纯文本,并将其中的一部分转换为html标记。我不需要完全丰富的编辑器,只需要以下几个标记:Javascript 使用js正则表达式替换简单的标记样式,如**粗体**到<;b>;粗体</b>;,javascript,regex,text,Javascript,Regex,Text,我试图获取一块纯文本,并将其中的一部分转换为html标记。我不需要完全丰富的编辑器,只需要以下几个标记: **bold** __underline__ ~~italics~~ --strike-- <<http://www.link.com>> **粗体** __下划线__ ~~斜体~~ --罢工-- 这是我尝试编写的方法,但由于缺少regex/js,它似乎无法实现: function toMarkup($this) { var text = $this.te
**bold**
__underline__
~~italics~~
--strike--
<<http://www.link.com>>
**粗体**
__下划线__
~~斜体~~
--罢工--
这是我尝试编写的方法,但由于缺少regex/js,它似乎无法实现:
function toMarkup($this) {
var text = $this.text();
text = text.replace("\*\*(.*)\*\*", "<b>$1</b>");
text = text.replace("__(.*)__", "<u>$1</u>");
text = text.replace("~~(.*)~~", "<i>$1</i>");
text = text.replace("--(.*)--", "<del>$1</del>");
text = text.replace("<<(.*)>>", "<a href='$1'>Link</a>");
$this.html(text);
}
函数标记($this){
var text=$this.text();
text=text.replace(“\*\*(.*)\*\*”,“$1”);
text=text.replace(“\\(.*)\\”,“$1”);
text=text.replace(“~~(.*)~”,“$1”);
text=text.replace(“--”(.*)-”,“$1”);
text=text.replace(“,”);
$this.html(文本);
}
关于这些替换不起作用的原因,有什么明显的错误吗?我刚刚意识到的另一个问题是,通过将此文本转换为html,我可以避免任何其他可能是恶意的标记。一个额外的好处是关于如何仅转义这些元素而不转义其他元素的任何建议。使用Regexp对象作为
text.replace()
的第一个参数,而不是字符串:
function toMarkup($this) {
var text = $this.text();
text = text.replace(/\*\*(.*?)\*\*/g, "<b>$1</b>");
text = text.replace(/__(.*?)__/g, "<u>$1</u>");
text = text.replace(/~~(.*?)~~/g, "<i>$1</i>");
text = text.replace(/--(.*?)--/g, "<del>$1</del>");
text = text.replace(/<<(.*?)>>/g, "<a href='$1'>Link</a>");
$this.html(text);
}
function toMarkup($this) {
var text = $this.text();
text = text.replace(/\*\*(.*?)\*\*/g, "<b>$1</b>");
text = text.replace(/__(.*?)__/g, "<u>$1</u>");
text = text.replace(/~~(.*?)~~/g, "<i>$1</i>");
text = text.replace(/--(.*?)--/g, "<del>$1</del>");
text = text.replace(/<<(.*?)>>/g, "<a href='$1'>Link</a>");
$this.html(text);
}
函数标记($this){
var text=$this.text();
text=text.replace(/\*\*(.*?\*\*\*/g,“$1”);
text=text.replace(/_uuuuuuuuuuuuuuuu(.*?)\uuuuuuug,“$1”);
text=text.replace(/~(.*?)~/g,“$1”);
text=text.replace(/--(.*?)--/g,“$1”);
text=text.replace(//g,“”);
$this.html(文本);
}
请注意,我还将所有的
*
替换为*?
,它将匹配尽可能少的字符,否则您的匹配可能太长。例如,您可以从第一个***
匹配到最后一个***
,而不是在下一个***
处停止。正则表达式还需要g
标志,以便替换所有匹配项(谢谢Aaron)。您使用的正则表达式语法是字符串文字语法,而不是正则表达式文字语法
function toMarkup($this) {
var text = $this.text();
text = text.replace(/\*\*(.*)\*\*/g, "<b>$1</b>");
text = text.replace(/__(.*)__/g, "<u>$1</u>");
text = text.replace(/~~(.*)~~/g, "<i>$1</i>");
text = text.replace(/--(.*)--/g, "<del>$1</del>");
text = text.replace(/<<(.*)>>/g, "<a href='$1'>Link</a>");
$this.html(text);
}
函数标记($this){
var text=$this.text();
text=text.replace(/\*\*(.*)\*\*/g,“$1”);
text=text.replace(/_u;(.*)_;/g,“$1”);
text=text.replace(/~(.*)~/g,“$1”);
text=text.replace(/--(.*)-/g,“$1”);
text=text.replace(//g,“”);
$this.html(文本);
}
如果要从字符串创建regexp,需要使用regexp
构造函数,但还需要转义\
字符,以便在regex中获得反斜杠
您可能还应该使您的
*
不贪婪<代码>*?首先,它们只是字符串,而不是正则表达式。其次,您应该使用not greedy*
此外,您可能希望使用g
修饰符匹配文本中的每个事件
函数标记($this){
var text=$this.text();
text=text.replace(/\*\*(.*?\*\*\*/g,“$1”);
text=text.replace(/_uuuuuuuuuuuuuuuu(.*?)\uuuuuuug,“$1”);
text=text.replace(/~(.*?)~/g,“$1”);
text=text.replace(/--(.*?)--/g,“$1”);
text=text.replace(//g,“”);
$this.html(文本);
}
标记功能($this){
$this.html($this.text().replace(/(| ~~~--|\*\*)(.*)\1\\/g,
功能(m、m1、m2、m3){
m[1]={'**':'b>','''.':'u>','-':'del>','~~':'i>'}[m[1]];
返回m[3]?“”
:(““任何关于这些替换不起作用的明显错误”。是的,因为这些不是正则表达式,只是字符串。不要忘记这些正则表达式上的/g修饰符。不要忘了将?
添加到第一个正则表达式中,使其变懒。谢谢你,我甚至没有注意到差异。这不允许使用**S***
,它应该是S*
@t。如果有必要,你可以使用它/\*\*(\***?\**)\*/
,但可读性不太好。最好的正则表达式应该是:/\*\*(\***[\s\s]*?\**)\*\*/g
。
function toMarkup($this) {
$this.html ($this.text ().replace (/(__|~~|--|\*\*)(.*?)\1|<<(.*?)>>\/g,
function (m, m1, m2, m3) {
m[1] = {'**' : 'b>', '__': 'u>', '--': 'del>', '~~': 'i>'}[m[1]];
return m[3] ? '<a href="' + m[3] + '">Link</a>'
: ('<' + m[1] + m[2] + '</' + m[1]);
});
}