Remove all part of a string from a specific string

Anisse Mahtat
Tera Contributor

Hi, 

 

I have a description like this for all my records :

des.PNG

But i want to remove from the description who is an HTML type variable all the part who start with 'Les champs suivants'.

How I can do that ? 

1 ACCEPTED SOLUTION

Mohammad Nayeem
Tera Expert

 

Lets say I want to remove the entire line starting with "This is Identifier" in the Description (HTML) field.

MohammadNayeem_0-1669234020588.png

 

First lets see how the data looks in this field.

var gr = new GlideRecord("u_experiment");
gr.get("f9fc3b798713d11045afb8c6cebb35af");
gs.print(gr.u_description);

Output:

<p>Here is sample text</p>
<p>This is Identifier - This content to be removed.</p>
<p>Theme: Information</p>
<p>Info: Sample Info</p>

 

Below code will remove the entire line

var gr = new GlideRecord("u_experiment");
gr.get("f9fc3b798713d11045afb8c6cebb35af");

var str = gr.u_description;
var start = str.indexOf("This is Identifier")-3;
var end = str.substr(start).indexOf("</p>")+4;
var temp = str.substr(start, end)

gr.u_description = str.replace(temp, '');
gr.update();

 Result:

MohammadNayeem_1-1669234464563.png

 

Use this to update multiple records

var gr = new GlideRecord("u_experiment");
gr.addQuery("u_description", "CONTAINS", "This is Identifier");
gr.query();
gs.print(gr.getRowCount());
var str = "", tempStr = "", start = 0, end = 0;

while(gr.next())
{
  str = gr.u_description;
  start = str.indexOf("This is Identifier")-3;
  end = str.substr(start).indexOf("</p>")+4;
  tempStr = str.substr(start, end)

  gr.u_description = str.replace(tempStr, '');
  gr.update();
}

View solution in original post

2 REPLIES 2

Pratik Malviya
Tera Guru

Hi @Anisse Mahtat ,

 

What exactly you want to remove here,

Only 'Les champs suivants'. word or all the details. Can you please provide more detail on it ?

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Thanks,
Pratik Malviya

Mohammad Nayeem
Tera Expert

 

Lets say I want to remove the entire line starting with "This is Identifier" in the Description (HTML) field.

MohammadNayeem_0-1669234020588.png

 

First lets see how the data looks in this field.

var gr = new GlideRecord("u_experiment");
gr.get("f9fc3b798713d11045afb8c6cebb35af");
gs.print(gr.u_description);

Output:

<p>Here is sample text</p>
<p>This is Identifier - This content to be removed.</p>
<p>Theme: Information</p>
<p>Info: Sample Info</p>

 

Below code will remove the entire line

var gr = new GlideRecord("u_experiment");
gr.get("f9fc3b798713d11045afb8c6cebb35af");

var str = gr.u_description;
var start = str.indexOf("This is Identifier")-3;
var end = str.substr(start).indexOf("</p>")+4;
var temp = str.substr(start, end)

gr.u_description = str.replace(temp, '');
gr.update();

 Result:

MohammadNayeem_1-1669234464563.png

 

Use this to update multiple records

var gr = new GlideRecord("u_experiment");
gr.addQuery("u_description", "CONTAINS", "This is Identifier");
gr.query();
gs.print(gr.getRowCount());
var str = "", tempStr = "", start = 0, end = 0;

while(gr.next())
{
  str = gr.u_description;
  start = str.indexOf("This is Identifier")-3;
  end = str.substr(start).indexOf("</p>")+4;
  tempStr = str.substr(start, end)

  gr.u_description = str.replace(tempStr, '');
  gr.update();
}