Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add spaces and line breaks to text 1

Status
Not open for further replies.

blasterstudios

Technical User
Dec 30, 2004
128
US
I'm using a prompt to get a user to type in some text. I need to add a space after each character they type as well as 2 line breaks where a natural space appears. for example, if the user types in John Smith, the variable should end up:
J
o
h
n

S
m
i
t
h

I already have the prompt popping up fine, I just need to do the space thing. Thanks.
 
>I need to add a space after each character they type as well as 2 line breaks where a natural space appears

I read it a couple of times and cannot verify it with your example given? Maybe you mean this?
[tt]"I need to add a [red]line break[/red] after each character they type as well as 2 line breaks where a natural space appears"[/tt]

This is how you can do it if that's the case.
[tt]
var s,t;
s="John Smith" //this is the string you get from prompt
t=s.replace(/(\w(?=[^$]))/g,"$1\n");
t=t.replace(/(( |\t)(?=[^$]))/g,"\n"); //end transformed result needed
[/tt]
It is ready to place in some textarea or some pre-tag contents with effect like the example. If you need to place it in the page somewhere in a span or div or else, use <br /> rather than \n in the replacement (2nd parameter).
 
Sorry about the confusion. I did mean space, however the place i have it is very skinny (9 px), so it will automatically wrap after each letter. I guess what I should have done is:
J o h n

S m i t h

But I meant what I said...
 
Also, this text is going inside the textarea so i do need the /n.
 
In that case, do it like this on the same line of construction as what I posted.
[tt]
var s,t;
s="John Smith" //this is the string you get from prompt
t=s.replace(/(( |\t)(?=[^$]))/g,"\n\n");
t=t.replace(/(\w(?=[^$]))/g,"$1 "); //end transformed result needed
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top