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

javascript - Replace only "CRLF" in string having both "CRLF" and "LF" lin

programmeradmin4浏览0评论

I have a string containing both "CRLF" and "LF" as line separator.

I want to replace only "CRLF" with " LF" (space and LF) and keeping "LF" as it is.

I know,we can do this using regex, but I have tried few regex from the already existing answers, but not able to resolve.

Please help, I am new to the javascript/jQuery.

I have a string containing both "CRLF" and "LF" as line separator.

I want to replace only "CRLF" with " LF" (space and LF) and keeping "LF" as it is.

I know,we can do this using regex, but I have tried few regex from the already existing answers, but not able to resolve.

Please help, I am new to the javascript/jQuery.

Share Improve this question edited Feb 6, 2016 at 21:14 marc_s 754k184 gold badges1.4k silver badges1.5k bronze badges asked Jun 18, 2015 at 6:46 RahulRahul 3791 gold badge3 silver badges11 bronze badges 3
  • 2 Can you please provide the string and the code you have tried. Also tag please your question properly with regex and remove any tags if needed. – cezar Commented Jun 18, 2015 at 6:54
  • You should post your regexp. Basically -> CR => \r LF => \n Then : var regexp_clean_CRLF = new RegExp("\r\n", "g"); my_string = my_string.replace(regexp_clean_CRLF, " \n") – Waxo Commented Jun 18, 2015 at 6:55
  • I cannot upload any input data right now as am in office and upload is banned, will upload the screenshot of the input string. – Rahul Commented Jun 18, 2015 at 7:11
Add a comment  | 

1 Answer 1

Reset to default 21

As simple as

the_string.replace(/\r\n/g, "\n");

where \r is the escape sequence for CR and \n is the one for LF and g is a regex modifier to replace all matching instances instead of only the first one found.

发布评论

评论列表(0)

  1. 暂无评论