Not sure where to start..
First off, you might want to look at this response as to whether using regexes for parsing markdown to HTML is safe.response as to whether using regexes for parsing markdown to HTML is safe. You are of course not parsing HTML, but the sentiment is still valid in my mind.
Second off, there is this and this on github, these parsers are a few hundred lines. You are missing use cases in your 30 lines of code. That is fine for a hobby site, not for a production site.
If you have doubts, you can always run MD tests on your code and see for your self.
Other than that;
If you insist on using regexes, at the very least add a line of comment of what you are trying to achieve, ideally add a second line with an example
Modifying standard objects, enough said..
Single letter variables (
f = b.substr(0, 2);
) are very unfortunate0 lines of comment together with the previous comment make for terrible code
html()
-> If you learn 1 thing today, it should be that mere mortals (like you and me) should not write escapers or parsers from scratch, instead we should figure out how smarter people did it and re-use their work. In jQuery, the authors call the DOM method.createTextNode()
, which replaces special characters with their HTML entity equivalents.In jQuery, the authors call the DOM method.createTextNode()
, which replaces special characters with their HTML entity equivalents. If you are running this server side as per your comment, then I would suggest you look up how Chromium does it. You did well and only forgot about{ '\'', "'" },
Assignments in
if
statements are considered bad form:while (typeof(i = find.shift()) == 'string' && typeof(j = replace.shift()) == 'string') t = t.replaceAll(i || '', j || '');
To turn this into good code, you will have to roll this out to something like
while ( find.length ){ caterpillar = find.shift(); butterfly = replace.shift(); if( typeof caterpillar != 'string' || typeof butterfly != 'string' ) break; //Turn all caterpillars into butterflies this = this.replaceAll( caterpillar , butterfly ); }
All in all, you are re-inventing the wheel. If you want to continue with this code then you have your work cut out..