jquery - match() textarea value then append words to a div mantaining their original order -
i'm trying regex match textarea value extract words, append them div mantaining original order.. order not respected..
if have in textarea:
<textarea> http://example.com http://example.com http://example.com </textarea>
after apply match, (match1:/http/g
) , (match2:/:\/\//g
), (match3:/example/g
) obtain this:
<div id="mydiv"> <span>http</span><span>://</span><span>example.com</span> <span>http</span><span>://</span><span>example.com</span> <span>http</span><span>://</span><span>example.com</span> </div>
but i'm using code:
function myfun(ematch, etype, eclass){ var split = $('#mytextarea').val().match(ematch); (var = 0; < split.length; i++){ if(split[i]){ $('body').append('<'+etype+' class="'+eclass+'">'+split[i]+'</'+etype+'>'); } } } myfun(/http/g, "span", "blue"); myfun(/:\/\//g, "span", "green"); myfun(/google/g, "span", "red");
and not maintain original order... there way maintain original order?
here's jsfiddle demo
updated:
take @ code , see if want: demo
function myfun(ematch, etype, eclass,string){ var split = string.match(ematch); if(split==null) homecoming false; (var = 0; < split.length; i++){ if(split[i]){ $('body').append('<'+etype+' class="'+eclass+'">'+split[i]+'</'+etype+'>'); } } } var lines=$('#mytextarea').val().split(/\n/); var lineslength=lines.length; if(typeof lines[lineslength]=='undefined') lineslength--; var firstval=$('#mytextarea').val(); $('body').append('<br>'); for(i=0;i<=lineslength;i++){ myfun(/http/g, "span","blue",lines[i]); myfun(/:\/\//g, "span","green",lines[i]); myfun(/google/g, "span","red",lines[i]); myfun(/.com/g, "span","red",lines[i]); $('body').append('<br>') }
you can see i've changed logic bit, need pass string function, in order create work elements' texts, values , variables.
what code value of textarea line line, , applies replacements each line, values in order.
jquery order append match
No comments:
Post a Comment