使用正则表达式将 Textile 斜体标记转换为 HTML

发布于 2024-12-09 11:21:08 字数 1265 浏览 0 评论 0原文

我继承了一个 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 &amp; 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 &amp; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠意 2024-12-16 11:21:08

试试这个:

var str  = 'Hello this is some _italic text_ here.'; 
var str = str.replace(/_([^_]+)_/,"<em>$1</em>");

如果您有很多文本要制作为斜体,则必须在正则表达式中使用“g”标志来获取所有匹配项。

Try this:

var str  = 'Hello this is some _italic text_ here.'; 
var str = str.replace(/_([^_]+)_/,"<em>$1</em>");

if you have a lots of text to make as intalic,you must use the "g" flag in you regexp to get all matches.

|煩躁 2024-12-16 11:21:08
<script type="text/javascript">
    jQuery().ready(function() {
        jQuery("p.wp-caption-text").each(function(n) {
            this.innerHTML = this.innerHTML.replace(new RegExp("{link:([^}]*)}([^{]*){/link}"), "<a href=\"$1\">$2</a>");
            this.innerHTML = this.innerHTML.replace(new RegExp("{i}([^{]*){/i}"), "<i>$1</i>");
        });
    });
</script>

您不是替换 [link...]...[/link] 而是替换 {link...}...{/link} 并将其替换为 i 如果您想要 [..] 代替:

<script type="text/javascript">
    jQuery().ready(function() {
        jQuery("p.wp-caption-text").each(function(n) {
            this.innerHTML = this.innerHTML.replace(new RegExp("\[link:([^\[]*)\]([^\[]*)\[/link\]"), "<a href=\"$1\">$2</a>");
            this.innerHTML = this.innerHTML.replace(new RegExp("\[i\]([^\[]*)\[/i\]"), "<i>$1</i>");
        });
    });
</script>

也许有一个拼写错误

抱歉。 !

对于我不阅读问题的态度:)

请参阅其他。它的工作原理

以及使用 g 的情况:

var str  = 'Hello this is some _italic text_ here.'; 
var str = str.replace(/_([^_]+)_/g,"<em>$1</em>");
<script type="text/javascript">
    jQuery().ready(function() {
        jQuery("p.wp-caption-text").each(function(n) {
            this.innerHTML = this.innerHTML.replace(new RegExp("{link:([^}]*)}([^{]*){/link}"), "<a href=\"$1\">$2</a>");
            this.innerHTML = this.innerHTML.replace(new RegExp("{i}([^{]*){/i}"), "<i>$1</i>");
        });
    });
</script>

You are not replacing [link...]...[/link] but {link...}...{/link} and the with i if you want [..] instead:

<script type="text/javascript">
    jQuery().ready(function() {
        jQuery("p.wp-caption-text").each(function(n) {
            this.innerHTML = this.innerHTML.replace(new RegExp("\[link:([^\[]*)\]([^\[]*)\[/link\]"), "<a href=\"$1\">$2</a>");
            this.innerHTML = this.innerHTML.replace(new RegExp("\[i\]([^\[]*)\[/i\]"), "<i>$1</i>");
        });
    });
</script>

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:

var str  = 'Hello this is some _italic text_ here.'; 
var str = str.replace(/_([^_]+)_/g,"<em>$1</em>");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文