javascript - Regex to get parameters of Embed -


code:

var = [  /<b\>(.*?)<\/b\>/ig,  /<i\>(.*?)<\/i\>/ig,   /<u\>(.*?)<\/u\>/ig,  /<br\/\>(.*?)/ig,  /<embed(.*?)/ig  ];   var b = [    '[b]$1[/b]',    '[i]$1[/i]',    '[u]$1[/u]',    '\r',    '[flash($2,$3)]$1[/flash]'   ];     (var =0;i<a.length;i++) {     ele = ele.replace(a[i], b[i]);      } 

html:

<embed   pluginspage="http://www.macromedia.com/go/getflashplayer"  src="http://www.youtube.com/v/dm0vrhuu6ei"  width="500"   height="500"   type="application/x-shockwave-flash"   wmode="transparent"   quality="high"   scale="exactfit"> 

what is

$1 starting position of [flash]

$2 width parameter

$3 height parameter

the beginning of code change html elements bbcodes. should go regex different in embed regex? see example here better understand!

http://jsbin.com/ajeqog/2/edit

as turns out - jquery has you're looking for, if know lesser used variants, fact $('<b>hello</b>') parses , returns html you've passed it. you're looking this:

var str = '<b>hello</b><br/><i>world</i><br/><u>i hate you!</u><br/><ul><li>good</li><li>goodbye</li><li>good afternoon</li><li>good marrow</li></ul> <embed pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://www.youtube.com/v/dm0vrhuu6ei" width="500" height="500" type="application/x-shockwave-flash" wmode="transparent" quality="high" scale="exactfit">';  var convertible = {     b: function(loc){         return '[b]' + converttobbcode($(loc).html()) + '[/b]';     }     , i: function(loc){         return '[i]' + converttobbcode($(loc).html()) + '[/i]';     } }; function converttobbcode(str){     if (!str || str == undefined) return '';     var toreturn = '';     $('<div>'+str+'</div>').contents().each(function(){         if (this.tagname == undefined) toreturn += this.nodevalue;         else if (convertible[this.tagname] != undefined)             toreturn += convertible[this.tagname](this);         else              toreturn += $(this).html(converttobbcode(this.innerhtml))[0].outerhtml;     });     return toreturn; }  $('textarea').val(converttobbcode(str)); 

i haven't done whole thing, because think it's pretty self evident what's being done. hardest part embed, becomes easy pie:

    , embed: function(loc){         var l = $(loc);         return '[flash(' + l.attr('width') + ',' + l.attr('height') + ')]' + l.attr('src') + '[/flash]';     } 

you can see partial solution here, in jsfiddle.

remember, should using regular expressions parse context sensitive language html. should listen when people tell you're approaching problem wrong - if have valid reason non-traditional approach, should spell out think traditional approach fails.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -