Concatenation of string having line breaks(\n)

neharao
Giga Contributor

Hi All,

I want to write a fix script to concatenate string having line breaks(\n) with space in-between. Please find the example below :

find_real_file.png

I want the above comments field to be updated as "abcd efgh ijkl mnop".

I have written a fix script which looks like below:

var gr = new GlideRecord("alm_hardware");      

gr.addQuery('display_name','P1000479 - Apple MacBook Pro 15"');       //As of now testing for one record

gr.query();      

while (gr.next()) {      

  gr.comments = gr.comments.replace(/\n/g, " ");      

      gs.log("check" + gr.comments);

  gr.update();      

}  

But the above script is just adding a space in front of the string and not concatinating.

can anyone help me on this.

Thanks,

Neha

3 REPLIES 3

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

try use this instead:


  gr.comments = gr.comments.replace(/(\r\n|\n|\r)/gm,"")


neharao
Giga Contributor

Hi Goran,



Thanks for the response. It worked as expected . I do not find the option to mark it correct hence marking it helpful.


Thanks again.



Thanks,


Neha


Perfect 😃



Strange, you should have a big "correct answer" at my reply to the left.. but most important that it worked 😃



//Göran