Suppose there is a large paragraph of text, the conversion rules are:
**title**
to<h1>title</h1>
:blush:
to<img class="emoji" title="blush" src="/img/blush.png"/>
How can I do the conversion in one traversal?
Suppose there is a large paragraph of text, the conversion rules are:
**title**
to <h1>title</h1>
:blush:
to <img class="emoji" title="blush" src="/img/blush.png"/>
How can I do the conversion in one traversal?
The best way to do such things is regex - it is very optimized mechanism for such tasks in every language.
in javascript according to your examples:
>>> "**title** qweqwe **foo** ololo **bar**".replace(/(\*\*(\w+)\*\*)/g, "<h1>$2</h1>")
"<h1>title</h1> qweqwe <h1>foo</h1> ololo <h1>bar</h1>"
and
":blush: qweqwe :tongue: ololo :smile:".replace(/(\:(\w+)\:)/g, '<img class="emoji" title="$2" src:="/img/$2.png"/>')
"<img class="emoji" title="blush" src:="/img/blush.png"/> qweqwe <img class="emoji" title="tongue" src:="/img/tongue.png"/> ololo <img class="emoji" title="smile" src:="/img/smile.png"/>"
tada!
you can try this :
<div id="replace">**title**some text :blush: some link </div>
var str= $("#replace").html();
str = str.replace("**title**", "<h1>title</h1>");
str = str.replace(":blush:", "<img class="emoji" title="blush" src:"/img/blush.png"/>");
$("#replace").html(str);
Capture the different patterns in 1 regex with different capture groups and have a replace-function inspect the different capture groups:
var input = '**Hey** Oh you! :blush:';
document.write(input.replace(/\*\*([^*]*)\*\*|:(happy|blush):/ig, function(match, title, smiley) {
if (title != undefined) {
return '<h2>' + title + '</h2>';
} else if (smiley != undefined) {
return '<img class="emoji" title="blush" src="/img/' + smiley + '.png" />';
}
}));
This is the only answer with just one traversal on this page. Note though that it won't catch this:
var text = '**title with :blush: smiley** gotcha!';
You can use a regex for this problem.
var mystring = 'some text **title** some other text';
var re = /\*{2}(\w*)\*{2}/g;
mystring.replace(re, "<h1>$1</h1>");
Here the regex searches for any pattern starting with two * and ending with two *. The middle part is captured and used in the replace method.
Title
var re_title = /\*\*(.+)\*\*/g;
str.replace(re_title, '<h1>$1</h1>')
Emoji
var re_emoji = /:(.+):/g;
str.replace(re_emoji, '<img class="emoji" title="$1" src="/img/$1.png"/>')
You can club them together by running one replace after the other.
str.replace(re_title, '<h1>$1</h1>').replace(re_emoji, '<img class="emoji" title="$1" src="/img/$1.png"/>')
Sample
var str = '**title** :some_emoji:'
str.replace(re_title, '<h1>$1</h1>').replace(re_emoji, '<img class="emoji" title="$1" src="/img/$1.png"/>')
// output = "<h1>title</h1> <img class="emoji" title="some_emoji" src="/img/some_emoji.png"/>"
var str = '**:some_emoji: **'
str.replace(re_title, '<h1>$1</h1>').replace(re_emoji, '<img class="emoji" title="$1" src="/img/$1.png"/>')
// output = "<h1><img class="emoji" title="some_emoji" src="/img/some_emoji.png"/></h1>"
Since the capture groups cannot be named, the 'a|b' regex syntax cannot be used for replacement.