Using Regular Expression to convert Textile italic markup to HTML

858 views Asked by At

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 &amp; 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 &amp; Zavattero; © Andrew Schoultz</p>

Any idea how the regexp could be modified to fix this?

2

There are 2 answers

1
The Mask On

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.

0
Andreas Louv On
<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>");