I want to replace some codes to dom elements. For example:
[emo]sad[emo] or [emo]sad[/emo]
to
<span class='smiley medium' data-emo='" + emo + "'></span>
like
<span class='smiley medium' data-emo='sad'></span>
Is it possible?
I want to replace some codes to dom elements. For example:
[emo]sad[emo] or [emo]sad[/emo]
to
<span class='smiley medium' data-emo='" + emo + "'></span>
like
<span class='smiley medium' data-emo='sad'></span>
Is it possible?
How about:
var s1 = "[emo]sad[emo]";
var s2 = "[emo]sad[/emo]";
var regex = /^\[(\w+)\](.+)\[\/?(\w+)\]$/; // matches s1 and s2
var replacement = "<span class='smiley medium' data-$1='$2'></span>";
var s1HTML = s1.replace(regex,replacement);
var s2HTML = s2.replace(regex,replacement);
// now append s1HTML and s2HTML to dom
Breaking down the reg ex:
var regex = /
^ // start
\[(\w+)\] // the first [] block, match inside the []
(.+) // match between the [] blocks
\[\/?(\w+)\] // the second [] block, optional "/", match inside the []
$ // end
/;
\[(\w+)\] with \[emo\] and \[\/?(\w+)\] with \[\/?emo\]split it into an array and then use a forEach loop