Reputation: 5025
var whatever = 'Some [b]random[/b] text in a [b]sentence.[/b]';
How can I replace every instance of [b]
with <b>
and every instance of [/b]
with </b>
in jQuery?
I was attempting to do it with regex but I couldn't get it to function properly.
Upvotes: 1
Views: 4604
Reputation: 43673
Elegant way:
whatever = whatever.replace(/\[(\/?)b\]/g,'<$1b>');
See and test it here.
Upvotes: 3
Reputation: 16020
With regex, it'd be:
whatever = whatever.replace(/\[b\]/g,'<b>').replace(/\[\/b\]/g,'</b>');
That'd seem to be the easiest solution
Upvotes: 1
Reputation: 26320
whatever = whatever.replace(/\[b\]/g, '<br>').replace(/\[\/b\]/g, '</b>');
Upvotes: 0
Reputation: 2541
string.replace("[b]", "<b>");
Failing that, you could use PHP to do it using str_replace
if it's being uploaded to a database.
Upvotes: 0