SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    USA
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with RegExp

    Hello!

    Here is what I need to be done, hope someone can help me.

    For example I have a string like this

    var string="It is a book That is a car Go get me some papers....";

    I need the function to search inside that string and insert new line \n front of Uppercase character.

    For example after run the function my string will have the value like this:

    It is a book
    That is a car
    Go get me some papers
    ....

    Below is my code but it doesn't work right

    Code:
    var string="It is a book That is a car Go get me some papers....";
    var new_string=string.replace(/\s[A-Z]/g,"\n"); 
    alert(new_string);
    The value of my new_string is:

    It is a book
    hat is a car
    o get me some papers
    ....

    missing the uppercase character. Please help me improve the code above

    Thanks

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    var str = 'It is a book That is a car Go get me some papers....';
    var tmp = str.replace(/([A-Z])/g, "\n$1");
    alert(tmp);


  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    USA
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. It works


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
  •