-1

Assuming that we have this variable string

const str = "address2\r\nmultiple1,\"my address row 1\nmy address row 2\",12345,\"my address 22222 row 1\nmy address 2222 row 2\"\r\n"

How to remove all the \n that is within the character \"? Maybe through a regex?

Expected Output should be

str = "address2\r\nmultiple1,my address row 1 my address row 2,12345,my address 22222 row 1 my address 2222 row 2\r\n"

2 Answers2

1

Using regex -

Regex

const str = "companyaddress2\r\nmultiple1,\"my address row 1\nmy address row 2\",12345,\"my address 22222 row 1\nmy address 2222 row 2\"\r\n";
const regex = new RegExp('(\\")([^\\n]*)(\\n)([^\\n]*)(\\")', 'g');
const newStr = str.replaceAll(regex, '$2 $4');
console.log(newStr);
Nikhil Patil
  • 2,480
  • 1
  • 7
  • 20
  • What does `$2 $4` mean? – steve_yuri852 Aug 02 '21 at 09:42
  • 1
    They are the sequence number of the matched groups in the regex. The regex has 5 groups enclosed in `()`. of that 1,3 and 5 are the ones that needs replacement and $2 $4 need to be kept same. 1 and 5 are completely removed and the 2 one is replaced with space, therefore -$2 $4. You can check the regex link for more explanation and also here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace – Nikhil Patil Aug 02 '21 at 09:49
-1

You can use:

str.replaceAll("\n", "");
Mehmet Ali
  • 313
  • 3
  • 15
  • This won't meet the spec - it will remove them all, regardless of position within string – MikeB Aug 02 '21 at 09:06