使用正则表达式将 Textile 斜体标记转换为 HTML
我继承了一个 Wordpress 网站,其中有几年的图像标题中的 Textile 标记。我不想将它们剥离出来,而是使用一些 jQuery 将已标记的斜体变成这样:
Hello this is some _italic text_ here.
谢谢
Hello this is some <em>italic text</em> here.
,这是我根据下面的答案尝试的方法。
<script type="text/javascript">
jQuery().ready(function() {
jQuery("p.wp-caption-text").each(function(n) {
this.innerHTML = this.innerHTML.replace(new RegExp("_([^{]*)_"), "<i>$1</i>");
});
});
</script>
它有效,但在我的标记中有时似乎找不到第二个 _ 字符。
这是一个现实世界的例子:
<p class="wp-caption-text">Left: Paul Klee, _Grosses Tier (Large Beast)_, 1928; fractional and promised gift of the Djerassi Art Trust. Right: Andrew Schoultz, _Three Caged Beasts_, 2011; Courtesy the artist and Marx & Zavattero; © Andrew Schoultz</p>
对于这个例子,它会产生这样的结果:
<p class="wp-caption-text">Left: Paul Klee, <i>Grosses Tier (Large Beast)_, 1928; fractional and promised gift of the Djerassi Art Trust. Right: Andrew Schoultz, _Three Caged Beasts</i>, 2011; Courtesy the artist and Marx & Zavattero; © Andrew Schoultz</p>
知道如何修改正则表达式来解决这个问题吗?
I inherited a Wordpress site with a few years worth of Textile markup in image captions. Rather than stripping them out I'd like to use some jQuery to turn italics that have been marked up like this:
Hello this is some _italic text_ here.
to this
Hello this is some <em>italic text</em> here.
Thanks, here is what I tried, based on the answers below.
<script type="text/javascript">
jQuery().ready(function() {
jQuery("p.wp-caption-text").each(function(n) {
this.innerHTML = this.innerHTML.replace(new RegExp("_([^{]*)_"), "<i>$1</i>");
});
});
</script>
It works, but in my markup sometimes it doesn't seem to find the second _ character.
Here is a real-world example:
<p class="wp-caption-text">Left: Paul Klee, _Grosses Tier (Large Beast)_, 1928; fractional and promised gift of the Djerassi Art Trust. Right: Andrew Schoultz, _Three Caged Beasts_, 2011; Courtesy the artist and Marx & Zavattero; © Andrew Schoultz</p>
For this one, it produces this:
<p class="wp-caption-text">Left: Paul Klee, <i>Grosses Tier (Large Beast)_, 1928; fractional and promised gift of the Djerassi Art Trust. Right: Andrew Schoultz, _Three Caged Beasts</i>, 2011; Courtesy the artist and Marx & Zavattero; © Andrew Schoultz</p>
Any idea how the regexp could be modified to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
如果您有很多文本要制作为斜体,则必须在正则表达式中使用“g”标志来获取所有匹配项。
Try this:
if you have a lots of text to make as intalic,you must use the "g" flag in you regexp to get all matches.
您不是替换
[link...]...[/link]
而是替换{link...}...{/link}
并将其替换为i
如果您想要[..]
代替:也许有一个拼写错误
抱歉。 !
对于我不阅读问题的态度:)
请参阅其他。它的工作原理
以及使用
g
的情况:You are not replacing
[link...]...[/link]
but{link...}...{/link}
and the withi
if you want[..]
instead:Maybe there is a typo
Sorry. !
For my NOT reading the question attitude :)
See the other. Its working
And what is been with using
g
: