View Full Version : Help with Script
nigeledge
09-23-2002, 07:22 PM
I'm trying to write a script which will go through a string and replace occurrances of spaces with and occurances of newlines (\n) with a <br> tag. I tried using the replace() method, but it only replaces the first occurance.
Any help would be greatly appreciated.
Thanks.
beetle
09-23-2002, 07:28 PM
the replace() method will work, but you need to use a pattern with the 'g' modifier (g is for global) for the search instead of a string
text = text.replace(/\n/g,"<br>");
nigeledge
09-23-2002, 08:15 PM
I knew it had to be something simple :)
Thanks for your help.
beetle
09-23-2002, 08:30 PM
no problem
Of course, I believe to make it fully compatible with all OSes, you'd need this
text = text.replace(/(\015\012)|(\015)|(\012)/g,"<br>");
Windows style newlines are like this:
<CR><LF> -> \r\n -> \015\012
Mac style like this:
<CR> -> \r -> \015
Unix style like this:
<LF> -> \n -> \012
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.