SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2000
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Splitting large piece of text?

    A bit of a strange problem I guess, the thing is that I have this large piece of text that I would like to use javascript to split into several parts of about 600 words each.

    Any ideas on how I should create such a javascript?

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This pretty much works. I just copied some text from an MSN article and set it to break it up every 5 words. I assumed only spaces, periods, commas, questions marks, and exclamations for word boundaries...may need more to suit your purposes. This also assumes that the source text contains no pipe characters.

    var txt = "Whether you're looking for peace of mind, protecting your investment or simply guarding your pocketbook, an extended service contract for your vehicle may be what you need. An extended service contract, commonly referred to as an extended warranty, is really an insurance policy that will pay for the cost of specific repairs to your vehicle.";
    txt = txt.replace(/(([ .,?!]?[^ .,?!]+[ .,?!]){5})/g,"$1|");
    txt = txt.split("|");

    // Test it
    alert(txt.join("\n\n"));
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Member
    Join Date
    Jan 2000
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks beetle, that's certainly something I can use .

    I'm planning on using this together with the IE Rich Text Editor and take the html code it generates and split it into x number of pages of around 600 words. I guess I might run into some problems with splitting the start/end of some html tags, but I can probably work around that somehow I guess.

    Well, thanks again, really appreciated.


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
  •