jquery - Word wrap function in Javascript -
i trying write javascript function takes 1, 2 or 3 word phrase , returns same phrase, first found space replaced <br>
so
hello world
becomes
hello<br>my world
or
hello world
becomes
hello<br>world.
ideas?
you equally change set of options, passing blanks works fine optional ordered params:
<div id='test'></div> <div id='test2'></div> <div id='test3'></div>
then in js:
function replaceme(sstring, starget, swith, ncount){ if(!sstring) return 'please provide string'; if(!starget) starget = /\s/; if(!swith) swith= '<br/>'; if(!ncount) ncount = 1; for(var c = 0; c < ncount; c++) sstring= sstring.replace(starget, swith); return sstring; } x = 'hello crazy world full of people!'; y = replaceme(x); document.getelementbyid('test').innerhtml = y; y = replaceme(x,'','',10); document.getelementbyid('test2').innerhtml = y; y = replaceme(x,'','',2); document.getelementbyid('test3').innerhtml = y;
you idea how becomes pretty flexible.
Comments
Post a Comment