Javascript : How to not confuse regex with BB code -
please note
i find several questions&answers end result came : dont use regex. posting question find solution problem. , maybe alternative regex. base on several similar question:
credit to: user2182349 sor resolving this. see answer below.
i'm trying replace bbcode. use function take answer still not working me.
i want replace things that:
var find = ["[b]", "[/b]", "[i]","[/i]","[u]","[/u]","[n]","[/n]","[c]","[/c]","[li]","[/li]"]; var rep = ["<b>", "</b>", "<i>","</i>","<u>","</u>","<div class='editnote'>","</div>","<center>","</center>","<li>","</li>",];
the [n] tag gets replaced span. once work span or other tags. use simple tag right able work.
the function , few sample string have tested
// escaped var fes = ["\[b\]", "\[/b\]","\[i\]", "\[/i\]"]; // without escape var f = ["[b]", "[/b]","[i]", "[/i]"]; //replaced var r = ["<b>", "</b>","<i>", "</i>"]; // string var string = "this [b]first[/b] 1 , here comes [b]second[/b] 1 , when text contain b bacon"; string.prototype.replacearray = function(find, replace) { var replacestring = this; var regex; (var = 0; < find.length; i++) { regex = new regexp(find[i], "g"); replacestring = replacestring.replace(regex, replace[i]); } return replacestring; }; //ignore rest. snippet work////////////////////////////////// $("#es").on("click",function (event) { string = string.replacearray(fes,r) $('#result').html(string); }); $("#noes").on("click",function (event) { string = string.replacearray(f,r) $('#result').html(string); }); $("#reset").on("click",function (event) { string = "this [b]first[/b] 1 , here comes [b]second[/b] 1 , when text contain b bacon"; $('#result').html(''); });
span{ cursor:pointer } div{ height:100px; width:300px; border:1px solid #000; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <span id="es">click here see result escape</span><br><br> <span id="noes">click here see result without escape</span><br><br> <span id="reset">reset here</span><br><br> <div id="result"></div>
so problem seams coming way send bbcode regex replacing function. how should feed it?
a regex nice because can add tags without lot of code. if understand question correctly, want convert bbcodes html equivalents. 1 way done regex.
var s = "this [b]first[/b] 1 , here comes [b]second[/b] 1 , when text contain b bacon, , there [i]icicles[/i]"; var re = /\[(\/?[bi])\]/mg; console.log (s.replace(re,'<$1>'));
if willing allow bbcodes, may use this:
var re = /\[(\/?[^\]]+)\]/mg;
this take [withsomecharacters] , turn <withsomecharacters>, including optional slash @ beginning.
a more complex implementation use array of objects. each object have 2 properties - in , out. in input, out output, , can loop through using string replace more complex tags. next piece of code regex.
var s = "this [n]editnote[/n] [b]first[/b] 1 , here comes [b]second[/b] 1 , when text contain b bacon, , there [i]icicles[/i] , [li]li[/li]"; var p,pairs = [ { "in": "[n]", "out": '<div class="editnote">' }, { "in": "[/n]", "out": '</div>' } ]; (p in pairs) { s = s.replace(pairs[p]["in"],pairs[p]["out"]); } var re = /\[(\/?[^\]]+)\]/mg; console.log (s.replace(re,'<$1>'));
Comments
Post a Comment