...

View Full Version : Only displaying whole words ...



snowball
04-11-2003, 07:51 PM
HI

Can anyone help?

I have some code which produces a variable called "head" containing the heading for a paragraph of text.

The heading must not be longer than 61 characters (including spaces) so I have used:

head = head.slice(head.indexOf('\s*')+1,head.indexOf('\s*')+61);


This works fine except if the heading is more than 61 chartacters it cuts it off mid word.

Can anyone come up with some code to follow what's above that would then remove any character(s) after the last space .....

I would really appreciate it.

Many thanks

snow ....

beetle
04-11-2003, 08:14 PM
head = head.replace( /^(.{61}).*$/, "$1" ).replace( /^(.*) [^ ]+$/, "$1" );

snowball
04-11-2003, 08:37 PM
Thanks for this ...... It is almost there, but not quite.

It is losing everything after the last space on all items not just the ones of more than 61 characters, so we now need to test to see if "head" is more than 61 characters, and only do the second line if it is:

head = head.slice(head.indexOf('\s*')+1,head.indexOf('\s*')+61);

head = head.replace( /^(.{61}).*$/, "$1" ).replace( /^(.*) [^ ]+$/, "$1" );


Does anyone know how to test the length to see if its more than 61 characters long, and then what the correct "IF" statement to put in would be .........

Thanks again .......

beetle
04-11-2003, 08:50 PM
if ( head.length > 61 )
...
else
....

I think the first replace I provided eliminates the need for your statement

snowball
04-11-2003, 10:11 PM
Thanks

That's excellent .............


snow ....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum