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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts

    RegEx - replace all spaces EXCEPT those followed by a hyphen

    Hello, everyone.

    I'm trying to write a Regular Expression replace script that will replace all spaces within a string with a space+, UNLESS the space is followed by a hyphen "-".

    I'm still a n00b with RegEx, despite the fact that I've been using RegEx for years. Just never had to do something like this, before.

    I've tried using /[ [^-]]/g and that doesn't work, as well as /[ {0,}-*/g. Any suggestions?

    Thanks,
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Have I understood you correctly? replace all spaces by a + unless the space is followed by a hyphen -.

    Code:
    <script type = "text/javascript">
    
    var t = "Peter - Paul and Percy are - senior coders";
    var nt = t.replace(/\s(?!-)/gi,"+");  // the ? is essential!
    
    alert (nt);
    
    </script>

    He has won six million dollars in as many years. - Presenter, Channel 5
    Last edited by Philip M; 06-27-2012 at 09:31 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Code:
    var re = / ([^\-])/g;
    var s = "try this demo -does it work?";
    var changed = s.replace( re, " +$1" );
    It works for all except a space at the very end of the string. We could handle that, too, if you cared.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Ahh... Philip's is more elegant, but he changed space to just + instead of space+, which I think is what you wanted. Trivial to change that if my interpretation is correct.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Old Pedant View Post
    Ahh... Philip's is more elegant, but he changed space to just + instead of space+, which I think is what you wanted. Trivial to change that if my interpretation is correct.
    Code:
    var nt = t.replace(/\s(?!-)/gi," +");  // the ? is essential!
    I was not sure of exactly what the OP required.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Thank you, both! I will give them both a shot and report back.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #7
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Brilliant!! Thank you, Philip M, it works exactly as I need (after adding a space before the plus).

    Yeah, I know.. odd.. why a space before the plus? To search a ColdFusion Solr collection (the + means all words will be required, not just OR, and I'm replacing all instances of "NOT" with a " -" before this point.)

    When I enter "air force NOT movement", the returned string is (again, exactly as I need it): " +air +force -movement". (Search for documents that have both air and force, but do not have the word movement.)

    Thanks, again!
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by WolfShade View Post
    Brilliant!! Thank you, Philip M, it works exactly as I need (after adding a space before the plus).

    Yeah, I know.. odd.. why a space before the plus? To search a ColdFusion Solr collection (the + means all words will be required, not just OR, and I'm replacing all instances of "NOT" with a " -" before this point.)

    When I enter "air force NOT movement", the returned string is (again, exactly as I need it): " +air +force -movement". (Search for documents that have both air and force, but do not have the word movement.)

    Thanks, again!
    Ah, I was wondering what the practical usage was!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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