none
Remove white-space before / after specified character RRS feed

  • Question

  • Very new to RegEx

    I need to remove whitespace before and after the slash "/" character in a string if it exists

    Ex
    find/ replace other stuff >> find/replace other stuff

    find /replace other stuff >> find/replace other stuff

    find / replace other stuff >> find/replace other stuffI am trying, .replace(/[/]+$/g, "")but it has no effect

    Trying this but no luck

     .replace(/[/]+$/g, "")

    Thanks
    Monday, July 9, 2018 5:17 PM

All replies

  • Try replacing "\s*[/]\s*" with "/".

    Or "\s+(?=[/])|(?<=[/])\s+" with "".



    • Edited by Viorel_MVP Monday, July 9, 2018 5:57 PM
    Monday, July 9, 2018 5:56 PM
  • You can do :

    string str = "find / replace other stuff";
    str = Regex.Replace(str, " */ *", "/");

    Monday, July 9, 2018 6:00 PM
  • Hello, and thanks for the responses

    I can get none of them to work, I am trying

              

        function test() {
          var t="find / replace"

          t = t.replace("\s*[/]\s*", "/");

          t = t.replace("\s+(?=[/])|(?<=[/])\s+", "");

          t = t.replace(" */ *", "/")

        }


    • Edited by Caposn Monday, July 9, 2018 6:56 PM
    Monday, July 9, 2018 6:55 PM
  • I found it

    t = t.replace(/\s*([\/])\s*/g, '$1');

    Monday, July 9, 2018 7:12 PM
  • Hi Capson,

    If your question has been solved, please mark the useful reply as answer. This will make answer searching easier in the forum and be beneficial to community members as well.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 10, 2018 7:45 AM
    Moderator