1

I want to insert newline character in regex replace what field.

For example:

Find With: (xyxyxy.*[\r\n]+)(yzyzyz)

Replace with: $1xyxyxy\r\nxyxyxy

also tried replace with: $1xyxyxy$r$nxyxyxy

None of them seems to be working.

Question is what is the mantra to insert carriage return and/or new line via regex replace.

Rahul
  • 10,830
  • 4
  • 53
  • 88
  • Are you sure it is JS? Not VBScript? In VBScript, you need `vbCrLf`: `"$1xyxyxy" & vbCrLf & "xyxyxy"` – Wiktor Stribiżew Feb 17 '16 at 09:29
  • Can you please show a sample input and the corresponding desired output? – nnnnnn Feb 17 '16 at 09:34
  • @WiktorStribiżew: I Think That is not good Idea. – Rahul Feb 17 '16 at 09:35
  • @nnnnnn: simple question: what is the regex character to insert newline. In .net \r\n will insert the newline. How to insert carriage return in the replace what field. – Rahul Feb 17 '16 at 09:37
  • @Rahul: why not a good idea? There is no *regex character to insert newline*. Newline sequence can be matched with special shorthand character class, but not in VBScript. Do you mean you do not want to hardcode the newline sequence? Then, try Tushar's approach: [`(xxxxxx.*(\r\n|\r|\n)+)` -> `$1$2$3`](https://regex101.com/r/rX9dY0/1). – Wiktor Stribiżew Feb 17 '16 at 09:40
  • This is waht I wanted to clarify. Means you can use \r\n in find what but not in replace with. \r\n can be used both in notepad++ and in .net regex. – Rahul Feb 17 '16 at 09:45
  • There isn't a regex character to *insert* anything. In JS you use a regex to *match* something, but the replacement you specify is just a string, not a regex (though the replacement string can use $1, etc., to specify captured groups). So if you want to insert a newline, you just include a newline in your replacement string. – nnnnnn Feb 17 '16 at 09:57
  • See tushar's comment. \n can be used there. Don't know much but his demo works. – Rahul Feb 17 '16 at 10:04
  • @nnnnnn: Oh sorry: I didn't see you (profile!) before commenting : please ignore my comment. – Rahul Feb 17 '16 at 10:05

1 Answers1

7

You can capture the newline characters and use that in replacement. By capturing the newline characters, you don't have to worry about differences in different OS representation of newline.

Find:

(xyxyxy.*(\r\n|\r|\n)+)(yzyzyz)

Replace:

$1$2$3

$2: Is the single newline character.

Tushar
  • 85,780
  • 21
  • 159
  • 179
  • Great work around. So \r\n (possible in .net regex) will work in JavaScript or vb-script regex correct? – Rahul Feb 17 '16 at 09:47
  • @Rahul That will work in any language/tool. Depending on how you use it, the backslashes might need to be doubled. – Tushar Feb 17 '16 at 09:49
  • ohh. Let me try, $1xyxyxy\\r\\nxyxyxy is not working either. – Rahul Feb 17 '16 at 09:51
  • @Rahul Check [Demo](https://regex101.com/r/sG0cX4/1). When using in replacement, you don't need to use two backslashes. Also see [Difference between \n and \r?](http://stackoverflow.com/questions/1761051/difference-between-n-and-r) – Tushar Feb 17 '16 at 09:54