Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Only displaying whole words ...

    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 ....

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    head = head.replace( /^(.{61}).*$/, "$1" ).replace( /^(.*) [^ ]+$/, "$1" );
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 .......

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if ( head.length > 61 )
    ...
    else
    ....

    I think the first replace I provided eliminates the need for your statement
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    New Coder
    Join Date
    Jun 2002
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks

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


    snow ....


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •