最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to insert carriage return in regex replace - Stack Overflow

programmeradmin0浏览0评论

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
 |  Show 4 more ments

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论