将标记转换为 HTML 的正则表达式
如何编写正则表达式将 mark 转换为 HTML? 例如,您可以输入以下内容:
This would be *italicized* text and this would be **bold** text
然后需要将其转换为:
This would be <em>italicized</em> text and this would be <strong>bold</strong> text
与 stackoverflow 使用的 mark down 编辑控件非常相似。
澄清
就其价值而言,我正在使用 C#。 另外,这些是我想要允许的唯一真实标签/降价。 转换的文本量将少于 300 个字符左右。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好的方法是找到移植到您正在使用的任何语言的 Markdown 库版本(您没有在问题中指定)。
既然您已经澄清您只希望处理 STRONG 和 EM,并且您使用的是 C#,我建议您查看 Markdown.NET 查看这些标签是如何实现的。 正如你所看到的,它实际上是两个表达式。 这是代码:
The best way is to find a version of the Markdown library ported to whatever language you are using (you did not specify in your question).
Now that you have clarified that you only want STRONG and EM to be processed, and that you are using C#, I recommend you take a look at Markdown.NET to see how those tags are implemented. As you can see, it is in fact two expressions. Here is the code:
单个正则表达式是不行的。 每个文本标记都有它自己的 html 翻译器。 更好地研究现有转换器的实现方式,以了解其工作原理。
http://en.wikipedia.org/wiki/Markdown#See_also
A single regex won't do. Every text markup will have it's own html translator. Better look into how the existing converters are implemented to get an idea on how it works.
http://en.wikipedia.org/wiki/Markdown#See_also
我不太了解 C#,但在 Perl 中它是:
I don't know about C# specifically, but in perl it would be:
我遇到了以下建议不要这样做的帖子。 就我而言,虽然我希望保持简单,但我想我会根据jop的建议发布此内容,以防其他人想要去做这个。
I came across the following post that recommends to not do this. In my case though I am looking to keep it simple, but thought I would post this per jop's recommendation in case someone else wanted to do this.