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 10 of 10

Thread: Line Break

  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Line Break

    I'm retreiving data from a texfile with AJAX through:

    document.getElementById("my div").innerHTML=xmlhttp.responseText;

    but I want to make a linebreak in the text before each capital letter starts. How do I go about doing that? Appreciate the help.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Could be a Regular Expression:
    Code:
    var text=xmlhttp.responseText.replace(/([\s])([A-Z])/g,'<br>$2');
    document.getElementById("mydiv").innerHTML=text;
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you - is it too much to ask for an explanation of

    (/([\s])([A-Z])/g,'<br>$2');

    ?

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Do you know Regular Expressions in JavaScript? If not, you should learn a little bit, before understanding the explanations:

    http://lawrence.ecorp.net/inet/samples/regexp-intro.php

    \s = a RegExp character matches a non-white-space
    [...] = matches "positively" a set
    (...) = groups the patterns in subsets
    [A-Z] = the set of the capital letters, from A to Z (English alphabet) is the ASCII order. A range.
    g = this flag makes the matches global (all the occurrences)
    $1, $2... $99 = refer the defined subsets (...) in their written order

    So:

    - ([\s]) matches a non-white-space in a set.
    - ([A-Z]) positioned immediately before a capital letter
    - g all over the string
    - keeps the second subset $2 (that means the Capital letters)
    - but replaces the first subset (the non-white-space) with a certain substring. A tag <br> in this case.

    The method is: string.replace(what,withWhat)


    Example:
    "Lorem Ipsum aedilit Pure"
    becomes:
    "Lorem<br>Ipsum aedilit<br>Pure"
    Last edited by Kor; 03-15-2011 at 04:30 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    lifestyle (03-15-2011)

  • #5
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I can also use (/([A-Z])/g, "<br />$1"); right?

    But why is it $1 and not $2 here?

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    $1 and $2 are placeholders for pattern matches in so called "grouping brackets ()"

    So the $1 matches the [A-Z] in your example. $2 matches the same in Kor's example. In his example $1 would match [\s]

  • Users who have thanked devnull69 for this post:

    lifestyle (03-15-2011)

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by lifestyle View Post
    I can also use (/([A-Z])/g, "<br />$1"); right?

    But why is it $1 and not $2 here?
    Because there is no $2 there. You have only a single subset defined. ([A-Z])

    It depends on what you want, precisely.

    But that will insert a <br> before all the capital letters within the string. Including the first one.

    Use something like that to test the RegExp solutions:
    Code:
    <script type="text/javascript">
    var x='LoRem Ipsum aedilit Pure'
    x=x.replace(/([A-Z])/g,'<br>$1')
    alert(x)
    </script>
    Code:
    <script type="text/javascript">
    var x='Lorem Ipsum aedilit Pure'
    x=x.replace(/([\s])([A-Z])/g,'<br>$2')
    alert(x)
    </script>
    Last edited by Kor; 03-15-2011 at 09:27 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    lifestyle (03-15-2011)

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by devnull69 View Post
    $1 and $2 are placeholders for pattern matches in so called "grouping brackets ()"
    Yes. That is their meaning. The limit is $99. But match is not quite the proper word. The $ token does not return anything. It points something. A sort of word like placefinger is, maybe, a better one.
    Last edited by Kor; 03-15-2011 at 09:38 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Ah ok, then I'll go with (/([A-Z])/g,'<br>$1') cause I need a new line before every capital letter. Thanks alot for your help!

  • #10
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As you want to make a linebreak in the text before each capital letter starts. So you should go through such a code:

    var text=xmlhttp.responseText.replace(/([\s])([A-Z])/g,'<br>$2');
    document.getElementById("mydiv").innerHTML=text;
    Last edited by WA; 03-23-2011 at 06:29 PM.


  •  

    Posting Permissions

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