我在图像标题中继承了一个带有几年纺织品标记的Wordpress网站。而不是剥离它们我想使用一些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>
知道如何修改正则表达式来解决这个问题吗?
答案 0 :(得分:0)
试试这个:
var str = 'Hello this is some _italic text_ here.';
var str = str.replace(/_([^_]+)_/,"<em>$1</em>");
如果您有许多要作为intalic的文本,则必须使用regexp中的“g”标志来获取所有匹配项。
答案 1 :(得分:0)
<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>");