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.
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.
Share Improve this question edited Feb 17, 2016 at 9:30 Rahul asked Feb 17, 2016 at 9:28 RahulRahul 11.6k5 gold badges61 silver badges97 bronze badges 9-
Are you sure it is JS? Not VBScript? In VBScript, you need
vbCrLf
:"$1xyxyxy" & vbCrLf & "xyxyxy"
– Wiktor Stribiżew Commented Feb 17, 2016 at 9:29 - Can you please show a sample input and the corresponding desired output? – nnnnnn Commented Feb 17, 2016 at 9:34
- @WiktorStribiżew: I Think That is not good Idea. – Rahul Commented Feb 17, 2016 at 9:35
- @nnnnnn: simple question: what is the regex character to insert newline. In \r\n will insert the newline. How to insert carriage return in the replace what field. – Rahul Commented Feb 17, 2016 at 9: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
. – Wiktor Stribiżew Commented Feb 17, 2016 at 9:40
1 Answer
Reset to default 7You 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.