nines
nines

Reputation: 521

Replace symbols with HTML-tags

I would like to replace symbols with HTMLtags. For example:

This :is: some :text: that could also look :a: bit :different:.

should become

This <span id="selected">is</span> some <span id="selected">text</span> that could also look <span id="selected">a</span> bit <span id="selected">different</span>.

Upvotes: 1

Views: 193

Answers (3)

Eugene Yarmash
Eugene Yarmash

Reputation: 149823

$s =~ s{:([^:]+):}{<span id="selected">$1</span>}g;

Upvotes: 2

Brian
Brian

Reputation: 4984

(:)(.+?)\1

using \1 will allow you to change out the ":" to other characters and make sure the closing matches.

Upvotes: 1

Deck
Deck

Reputation: 1979

$str = "This :is: some :text: that could also look :a: bit :different:";
$str =~ s/:(\w+):/<span id='selected'>$1<\/span>/g;
print $str;

Upvotes: 1

Related Questions