add/remove space after periods...

Discussion in 'Javascript' started by Joseph, Dec 18, 2004.

  1. Joseph

    Joseph Guest

    I have a textBox that people writes stories in it. They can use [bbcode]
    [/bbcode] for format.

    I have Aspell installed on the server, so people can make correction to
    their text. Sometimes, they forget to add a space after a period. So Aspell
    reads the last word of a sentence and the first word of the next sentence as
    a single world, which gives an error, word not found.

    Before sending the text to Aspell, i add a space after all the periods with:

    string = string.replace(/./g, ". ");

    I did this for the coma, and it's working, no problem.

    But i dont want to add a space after periods in certain [bbcode] [/bbcode].
    Because the links will not work.

    [link]http://www.tromal.net/index3.php[/link]
    [​IMG]

    I dont know how to do it, but after i added a space to all periods, i could
    search for "http" and remove all the space after the periods till the next
    "[".

    Or the space could only be added when the period is not between
    [link][/link] and

    Could someone help me on this one, thanks,

    André Couturier
    www.tromal.net
     
    Joseph, Dec 18, 2004
    #1
    1. Advertising

  2. Joseph

    RobB Guest

    Joseph wrote:
    > I have a textBox that people writes stories in it. They can use

    [bbcode]
    > [/bbcode] for format.
    >
    > I have Aspell installed on the server, so people can make correction

    to
    > their text. Sometimes, they forget to add a space after a period. So

    Aspell
    > reads the last word of a sentence and the first word of the next

    sentence as
    > a single world, which gives an error, word not found.
    >
    > Before sending the text to Aspell, i add a space after all the

    periods with:
    >
    > string = string.replace(/./g, ". ");
    >
    > I did this for the coma, and it's working, no problem.
    >
    > But i dont want to add a space after periods in certain [bbcode]

    [/bbcode].
    > Because the links will not work.
    >
    > [link]http://www.tromal.net/index3.php[/link]
    > [​IMG]
    >
    > I dont know how to do it, but after i added a space to all periods, i

    could
    > search for "http" and remove all the space after the periods till the

    next
    > "[".
    >
    > Or the space could only be added when the period is not between
    > [link][/link] and
    >
    > Could someone help me on this one, thanks,
    >
    > André Couturier
    > www.tromal.net



    Not really enough time to test this, but...

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>untitled</title>
    </head>
    <body>
    <pre>
    <script type="text/javascript">
    //<![CDATA[

    String.prototype.couturierFix = function()
    {
    var fixer = /([.,])/g;
    var refixer = /\. (?=[^\[]*\[\/(link|img)\])/gi;
    return this.replace(fixer, '$1 ').replace(refixer, '.');
    }

    var s = [

    'Lorem ipsum dolor sit amet,consectetur adipisicing elit,' ,
    'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' ,
    'Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris ' ,
    'nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in ' ,
    'reprehenderit in voluptate velit esse cillum dolore eu fugiat ' ,
    'nulla pariatur. Excepteur sint occaecat cupidatat non proident,' ,
    'sunt in culpa qui officia deserunt mollit anim id est laborum.' ,
    '[link]http://www.tromal.net/index3.php[/link]' ,
    '[​IMG]'

    ];

    s = s.join('\n');
    document.write(s.couturierFix());

    //]]>
    </script>
    </pre>
    </body>
    </html>

    [could write it as stand-alone routine too]
     
    RobB, Dec 18, 2004
    #2
    1. Advertising

  3. Joseph

    RobB Guest

    Typo: change to

    var fixer = /([.,])[\S]/g;

    Thanks.
     
    RobB, Dec 18, 2004
    #3
  4. Joseph

    RobB Guest

    Joseph wrote:
    > I have a textBox that people writes stories in it. They can use

    [bbcode]
    > [/bbcode] for format.
    >
    > I have Aspell installed on the server, so people can make correction

    to
    > their text. Sometimes, they forget to add a space after a period. So

    Aspell
    > reads the last word of a sentence and the first word of the next

    sentence as
    > a single world, which gives an error, word not found.
    >
    > Before sending the text to Aspell, i add a space after all the

    periods with:
    >
    > string = string.replace(/./g, ". ");
    >
    > I did this for the coma, and it's working, no problem.
    >
    > But i dont want to add a space after periods in certain [bbcode]

    [/bbcode].
    > Because the links will not work.
    >
    > [link]http://www.tromal.net/index3.php[/link]
    > [​IMG]
    >
    > I dont know how to do it, but after i added a space to all periods, i

    could
    > search for "http" and remove all the space after the periods till the

    next
    > "[".
    >
    > Or the space could only be added when the period is not between
    > [link][/link] and
    >
    > Could someone help me on this one, thanks,
    >
    > André Couturier
    > www.tromal.net


    This just in....

    String.prototype.couturierFix = function()
    {
    var fixer = /([.,])([\S])/g;
    var refixer = /\. (?=[^\[]*\[\/(link|img)\])/gi;
    return this.replace(fixer, '$1 $2').replace(refixer, '.');
    }

    Use like any other string method:
    textBoxRef.value = textBoxRef.value.couturierFix();
     
    RobB, Dec 19, 2004
    #4
  5. Joseph

    RobB Guest

    Joseph wrote:
    > I have a textBox that people writes stories in it. They can use

    [bbcode]
    > [/bbcode] for format.
    >
    > I have Aspell installed on the server, so people can make correction

    to
    > their text. Sometimes, they forget to add a space after a period. So

    Aspell
    > reads the last word of a sentence and the first word of the next

    sentence as
    > a single world, which gives an error, word not found.
    >
    > Before sending the text to Aspell, i add a space after all the

    periods with:
    >
    > string = string.replace(/./g, ". ");
    >
    > I did this for the coma, and it's working, no problem.
    >
    > But i dont want to add a space after periods in certain [bbcode]

    [/bbcode].
    > Because the links will not work.
    >
    > [link]http://www.tromal.net/index3.php[/link]
    > [​IMG]
    >
    > I dont know how to do it, but after i added a space to all periods, i

    could
    > search for "http" and remove all the space after the periods till the

    next
    > "[".
    >
    > Or the space could only be added when the period is not between
    > [link][/link] and
    >
    > Could someone help me on this one, thanks,
    >
    > André Couturier
    > www.tromal.net


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>untitled</title>
    </head>
    <body style="width:578px;margin:100px auto;font:9pt
    verdana;text-align:justify;background:#eef;">
    <script type="text/javascript">
    //<![CDATA[

    String.prototype.couturierFix = function()
    {
    return this.replace(/([.,])([\S])/g, '$1 $2').
    replace(/\. (?=[^[]*\[\/(link|img)\])/gi, '.');
    }

    var s = [

    'Lorem ipsum dolor sit amet,consectetur adipisicing elit,' ,
    'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' ,
    'Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris ' ,
    'nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in ' ,
    'reprehenderit in voluptate velit esse cillum dolore eu fugiat ' ,
    'nulla pariatur. Excepteur sint occaecat cupidatat non proident,' ,
    'sunt in culpa qui officia deserunt mollit anim id est laborum.' ,
    '<br /><br />[link]http://www.tromal.net/index3.php[/link]' ,
    '<br /><br />[​IMG]<br />'

    ];

    document.write(

    '|before|' ,
    '<br /><br />' ,
    (s = s.join('')) ,
    '<hr /><br />' ,
    '|after|<br /><br />' ,
    s.couturierFix()

    );

    //]]>
    </script>
    </body>
    </html>

    Think that helps. Use like any other string method:
    textBoxRef.value = textBoxRef.value.couturierFix();
     
    RobB, Dec 19, 2004
    #5
  6. Joseph

    RobB Guest

    Joseph wrote:
    > I have a textBox that people writes stories in it. They can use

    [bbcode]
    > [/bbcode] for format.
    >
    > I have Aspell installed on the server, so people can make correction

    to
    > their text. Sometimes, they forget to add a space after a period. So

    Aspell
    > reads the last word of a sentence and the first word of the next

    sentence as
    > a single world, which gives an error, word not found.
    >
    > Before sending the text to Aspell, i add a space after all the

    periods with:
    >
    > string = string.replace(/./g, ". ");
    >
    > I did this for the coma, and it's working, no problem.
    >
    > But i dont want to add a space after periods in certain [bbcode]

    [/bbcode].
    > Because the links will not work.
    >
    > [link]http://www.tromal.net/index3.php[/link]
    > [​IMG]
    >
    > I dont know how to do it, but after i added a space to all periods, i

    could
    > search for "http" and remove all the space after the periods till the

    next
    > "[".
    >
    > Or the space could only be added when the period is not between
    > [link][/link] and
    >
    > Could someone help me on this one, thanks,
    >
    > André Couturier
    > www.tromal.net


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>untitled</title>
    </head>
    <body style="width:578px;margin:100px auto;font:9pt
    verdana;text-align:justify;background:#eef;">
    <script type="text/javascript">
    //<![CDATA[

    String.prototype.couturierFix = function()
    {
    return this.replace(/([.,])([\S])/g, '$1 $2').
    replace(/\. (?=[^[]*\[\/(link|img)\])/gi, '.');
    }

    var s = [

    'Lorem ipsum dolor sit amet,consectetur adipisicing elit,' ,
    'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' ,
    'Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris ' ,
    'nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in ' ,
    'reprehenderit in voluptate velit esse cillum dolore eu fugiat ' ,
    'nulla pariatur. Excepteur sint occaecat cupidatat non proident,' ,
    'sunt in culpa qui officia deserunt mollit anim id est laborum.' ,
    '<br /><br />[link]http://www.tromal.net/index3.php[/link]' ,
    '<br /><br />[​IMG]<br />'

    ];

    document.write(

    '|before|' ,
    '<br /><br />' ,
    (s = s.join('')) ,
    '<hr /><br />' ,
    '|after|<br /><br />' ,
    s.couturierFix()

    );

    //]]>
    </script>
    </body>
    </html>

    Think that helps. Use like any other string method:
    textBoxRef.value = textBoxRef.value.couturierFix();
     
    RobB, Dec 19, 2004
    #6
  7. JRS: In article <AN1xd.71407$>, dated
    Sat, 18 Dec 2004 17:05:00, seen in news:comp.lang.javascript, Joseph
    <> posted :

    >I have a textBox that people writes stories in it. They can use [bbcode]
    >[/bbcode] for format.
    >
    >I have Aspell installed on the server, so people can make correction to
    >their text. Sometimes, they forget to add a space after a period. So Aspell
    >reads the last word of a sentence and the first word of the next sentence as
    >a single world, which gives an error, word not found.
    >
    >Before sending the text to Aspell, i add a space after all the periods with:
    >
    >string = string.replace(/./g, ". ");
    >
    >I did this for the coma, and it's working, no problem.
    >
    >But i dont want to add a space after periods in certain [bbcode] [/bbcode].



    There are many other cases where a space should not be added.

    There are 3,5 miles to go;
    there is .5 mile to go;
    this is news:comp.lang.javascript;
    in Ascii, one must write "..." for an ellipsis;
    etc., etc.

    Space correcting is not programmable, except possibly by experts in
    computing and language. Make the writers get it right, or use a human
    editor.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Dec 19, 2004
    #7
  8. Joseph

    RobB Guest

    Dr John Stockton wrote:

    (snip)

    > Space correcting is not programmable, except possibly by experts in
    > computing and language. Make the writers get it right, or use a

    human
    > editor.


    Doubtless so, but imperious nonetheless. Let them eat spaces.
    Work-in-progress, maybe useful to the OP:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>shoot me</title>
    <style type="text/css">

    body {
    font: normal 100% arial, sans-serif;
    background: #900;
    }
    #p1, #p2, #p3, #p4, #p5, #p6 {
    width: 340px;
    font: normal 70% arial, sans-serif;
    padding: 8px;
    margin: 0 auto;
    border: 1px #fff dashed;
    background: #faf0e0;
    }
    button {
    display: block;
    width: 100px;
    margin: 8px auto;
    }

    </style>
    <script type="text/javascript">
    //<![CDATA[

    String.prototype.couturierFix = function()
    {
    return this.replace(/(\D[.!?])(?=[^ .a-z\d])/g, '$1 ').
    replace(/,(?=[\S])/g, ', ').
    replace(/\. (?=[^[]*\[\/(link|img)\])/gi, '.');
    }

    function fixit()
    {
    var p, i = 1, fixed;
    while (p = document.getElementById('p' + i++))
    {
    fixed = p.firstChild.nodeValue.couturierFix();
    p.replaceChild(document.createTextNode(fixed), p.firstChild);
    }
    return false;
    }

    //]]>
    </script>
    </head>
    <body>
    <p id="p1">&nbsp;
    When a girl leaves her home at eighteen,she does one of two
    things.Either she falls into saving hands and becomes better, or she
    rapidly assumes the cosmopolitan standard of virtue and becomes worse.
    Of an intermediate balance, under the circumstances, there is no
    possibility.The city has its cunning wiles,no less than the infinitely
    smaller and more human tempter. There are large forces which allure
    with all the soulfulness of expression possible in the most cultured
    human. The gleam of a thousand lights is often as effective as the
    persuasive light in a wooing and fascinating eye. Half the undoing of
    the unsophisticated and natural mind is accomplished by forces wholly
    superhuman. A blare of sound, a roar of life, a vast array of human
    hives,appeal to the astonished senses in equivocal terms. Without a
    counsellor at hand to whisper cautious interpretations,what falsehoods
    may not these things breathe into the unguarded ear!Unrecognised for
    what they are, their beauty, like music, too often relaxes,then
    weakens,then perverts the simpler human perceptions.
    </p>
    <p id="p2">
    Link test: [​IMG]
    </p>
    <p id="p3">
    Image test: [​IMG]
    </p>
    <p id="p4">
    Question mark test: fixes question marks?Hopefully...
    </p>
    <p id="p5">
    Exclamation point test: Exclamation points!Yes/no?Maybe?
    </p>
    <p id="p6">
    Decimal / ellipsis test: there is .5 mile to go...correct?
    </p>
    <button onclick="return fixit()">fix it</button>
    <button onclick="location.reload()">restore</button>
    </body>
    </html>
     
    RobB, Dec 19, 2004
    #8
  9. Joseph

    Joseph Guest

    "RobB" <> wrote in message
    news:...
    > Dr John Stockton wrote:
    >
    > (snip)
    >
    > > Space correcting is not programmable, except possibly by experts in
    > > computing and language. Make the writers get it right, or use a

    > human
    > > editor.

    >
    > Doubtless so, but imperious nonetheless. Let them eat spaces.
    > Work-in-progress, maybe useful to the OP:
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    > "http://www.w3.org/TR/html4/strict.dtd">
    > <html>
    > <head>
    > <title>shoot me</title>
    > <style type="text/css">
    >
    > body {
    > font: normal 100% arial, sans-serif;
    > background: #900;
    > }
    > #p1, #p2, #p3, #p4, #p5, #p6 {
    > width: 340px;
    > font: normal 70% arial, sans-serif;
    > padding: 8px;
    > margin: 0 auto;
    > border: 1px #fff dashed;
    > background: #faf0e0;
    > }
    > button {
    > display: block;
    > width: 100px;
    > margin: 8px auto;
    > }
    >
    > </style>
    > <script type="text/javascript">
    > //<![CDATA[
    >
    > String.prototype.couturierFix = function()
    > {
    > return this.replace(/(\D[.!?])(?=[^ .a-z\d])/g, '$1 ').
    > replace(/,(?=[\S])/g, ', ').
    > replace(/\. (?=[^[]*\[\/(link|img)\])/gi, '.');
    > }
    >
    > function fixit()
    > {
    > var p, i = 1, fixed;
    > while (p = document.getElementById('p' + i++))
    > {
    > fixed = p.firstChild.nodeValue.couturierFix();
    > p.replaceChild(document.createTextNode(fixed), p.firstChild);
    > }
    > return false;
    > }
    >
    > //]]>
    > </script>
    > </head>
    > <body>
    > <p id="p1">&nbsp;
    > When a girl leaves her home at eighteen,she does one of two
    > things.Either she falls into saving hands and becomes better, or she
    > rapidly assumes the cosmopolitan standard of virtue and becomes worse.
    > Of an intermediate balance, under the circumstances, there is no
    > possibility.The city has its cunning wiles,no less than the infinitely
    > smaller and more human tempter. There are large forces which allure
    > with all the soulfulness of expression possible in the most cultured
    > human. The gleam of a thousand lights is often as effective as the
    > persuasive light in a wooing and fascinating eye. Half the undoing of
    > the unsophisticated and natural mind is accomplished by forces wholly
    > superhuman. A blare of sound, a roar of life, a vast array of human
    > hives,appeal to the astonished senses in equivocal terms. Without a
    > counsellor at hand to whisper cautious interpretations,what falsehoods
    > may not these things breathe into the unguarded ear!Unrecognised for
    > what they are, their beauty, like music, too often relaxes,then
    > weakens,then perverts the simpler human perceptions.
    > </p>
    > <p id="p2">
    > Link test: [​IMG]
    > </p>
    > <p id="p3">
    > Image test: [​IMG]
    > </p>
    > <p id="p4">
    > Question mark test: fixes question marks?Hopefully...
    > </p>
    > <p id="p5">
    > Exclamation point test: Exclamation points!Yes/no?Maybe?
    > </p>
    > <p id="p6">
    > Decimal / ellipsis test: there is .5 mile to go...correct?
    > </p>
    > <button onclick="return fixit()">fix it</button>
    > <button onclick="location.reload()">restore</button>
    > </body>
    > </html>
    >


    ====================
    Your hot! Thank-you very much for the script. I dont understand it, but it
    works great ! :eek:)

    Here is the script that i use for formating and preventing abuse when people
    writes stories:


    string = document.formname.story.value;

    // check for banned words
    var bannedWordArray = new Array(" pa ", " ki ", " ds "," ke "," g "," tro
    "," fo "," kan "," tou "," lè "," tjrs "," koi "," kelke "," pe "," ya ","
    dla "," po "," ns "," ù "," jvous "," keske ", " pr ", " moa ", " pk ", " mé
    ", " mem ", " ossi ");
    var bannedWordCount = 26;
    for (i = 0; i <= bannedWordCount; i++) {
    var bannedWordDetected = string.indexOf(bannedWordArray);
    if (bannedWordDetected != -1) { var messageError = 'banned words
    detected';}
    }

    // remove repeating characters
    string = stripToLong(string);

    // format text for periods and commas
    string = string.couturierFix();

    // remove double space
    string = string.replace(/ /g, " ");

    // remove space before commas
    string = string.replace(/ ,/g, ",");
    // end


    // -------------- functions

    // format text for period, comma
    String.prototype.couturierFix = function() {
    return this.replace(/(\D[.!?])(?=[^ .a-z\d])/g, '$1 ').
    replace(/,(?=[\S])/g, ', ').
    replace(/ \.(?=[\S])/g, '.').
    replace(/\. (?=[^[]*\[\/(lien|image)\])/gi, '.');
    }


    // limit repeating characters
    function stripToLong(string) {
    var accumulator = new Array();
    var current = "";
    var count = 0;
    for (i = 0;i < string.length; i++) {
    var c = string.charAt(i);
    if (c == current) {
    count++;
    } else {
    count = 1;
    current = c;
    }
    if (count <= 4) {
    accumulator = c;
    }
    }
    return accumulator.join("");
    }

    -----------------

    Thanks again,
    André Couturier
    www.tromal.net
     
    Joseph, Dec 20, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,914
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,680
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    1,030
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    730
    Michael Hudson
    Oct 24, 2003
  5. Joseph

    add/remove space after periods...

    Joseph, Dec 18, 2004, in forum: Javascript
    Replies:
    0
    Views:
    146
    Joseph
    Dec 18, 2004
Loading...

Share This Page