Search code examples
javascriptstringsplitnewlineaura-framework

\r\n not splitting string in javascript


I have a string which is multiline. I update this on SFL case object field. When i extract the same from database using SOQL, it appends \r\n\r\n whenever multiline text is found. I tried using different regex expressions with split(\r\n) or split(\r\n) and some more as suggested in different articles, but none of them are helping.

split(\r\n) it removes \r\n from my string but replaces it with ,,

What approach should i take.

SFL input--> This is line 1
             This is line 2

Output--> This is line 1 \r\n\r\n  This is line 2
when using split output--> This is line 1 ,,This is line 2

expected-->
This is line 1
This is line 2

Please help.


Solution

  • Use a character set in the regular expression separator.

    const string = 'This is line 1\r\n\r\nThis is line 2'
    const result = string.split(/[\r\n]+/)
    console.log(result)