Results 1 to 4 of 4

Thread: Capital letters

  1. #1
    Join Date
    Apr 2007
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Capital letters

    Hello,

    is it possible to have JavaScript divide a word, based on Capital Letters.
    Say the word "HelloWorld" is written into a file. JavaScript would then divide it into the two words "Hello World" ?

    If it is possible, could you show me how.

    Thank you, Marringi

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Try this:
    Code:
    var separated = "HelloWorld".replace(/(\S)([A-Z])/g, "$1 $2");
    Trinithis

  3. The Following User Says Thank You to Trinithis For This Useful Post:

    marringi (03-22-2008)

  4. #3
    Join Date
    Apr 2007
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thank you for the answer. Could you also show me how you would use it with "document.write" ?

    Thank you

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Document.write should only be used as the page is being loaded (parsed), otherwise it will overwrite the page, hopefully you understand this. Assuming that you do, you could do:

    Code:
    var separated = "HelloWorld".replace(/(\S)([A-Z])/g, "$1 $2");
    document.write(separated);
    In place of "HelloWorld" in the above, you could use any variable containing a string value:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <script type="text/javascript">
    var bob='NannyMcFee';
    var separated = bob.replace(/(\S)([A-Z])/g, "$1 $2");
    document.write(separated);
    </script>
    </body>
    </html>
    Which also demonstrates that this is not always the optimal way to parse a string with no spaces.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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