regexstring

Regex for removing leading and trailing carriage returns and line feeds from a sentence


Also is it possible to combine this with removing periods from within the string? The sentence may have spaces which I'd like to keep.


Solution

  • Search for

    ^[\r\n]+|\.|[\r\n]+$
    

    and replace with nothing.

    The specific syntax will depend on the language you're using, e. g. in C#:

    resultString = Regex.Replace(subjectString, @"^[\r\n]+|\.|[\r\n]+$", "");
    

    in PHP:

    $result = preg_replace('/^[\r\n]+|\.|[\r\n]+$/', '', $subject);
    

    in JavaScript:

    result = subject.replace(/^[\r\n]+|\.|[\r\n]+$/g, "");
    

    etc...