Search code examples
regexpowershellreplaceend-of-line

RegexStorm.net: How to add \n in Replacement


I have this regex to match:

(\<div class=\"(?:right).*)(\<p)

This is the input string:

<div class='mid'>Nov 11, 2016</div>
<div class="right">xxx yyy zzz<p>11/11/16 - 13:41</p></div>

It matches OK. I want to insert \n into the replacement so that <p> is in another line, but couldn't find a way. These all fail:

$1\n$2
$1\\n$2
$1`n$2
$1``n$2
"$1`n$2"

I know, in Powershell, quote with backtick can used for this regex. "xxx `n yyy" . But how to do that in RegexStorm page?

Had tried inserting

\\n
`n
``n
`\n

Solution

  • As you already know, in PowerShell you can use the escape sequence `n in an expandable string literal to insert a line break:

    $inputString -replace '(\<div class=\"(?:right).*)(\<p)',"`$1`n`$2"
    

    For the form on RegexStorm.net, simply put a literal linebreak (hit the ENTER key on your keyboard) between two replacement references, eg $1<linebreak goes here>$2:

    $1
    $2
    

    Screenshot of linebreak in replacement:

    enter image description here

    This also works in PowerShell:

    enter image description here