Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

While Copying HTML field to Journal field using Client Script and removing HTML Tags &nbsp is seen?

VIKAS45
Tera Guru

While Copying HTML field to Journal field using Client Script and removing HTML Tags &nbsp is still  seen?

// Get the HTML field value
var htmlFieldValue = g_form.getValue('html_field');

// Remove HTML tags using a regular expression
var plainTextContent = htmlFieldValue.replace(/<[^>]*>/g, '');

// Set the plain text content to the Journal field
g_form.setValue('journal_field', plainTextContent);

 

How to remove &nbsp which is still seen after copying?

2 ACCEPTED SOLUTIONS

Jaspal Singh
Mega Patron
Mega Patron

Can you try below.

/ Get the HTML field value
var htmlFieldValue = g_form.getValue('html_field');

// Remove HTML tags using a regular expression
var plainTextContent = htmlFieldValue.replace(/<[^>]*>/g, '');

//remove &nbsp
var plainTextContentis=plainTextContent.replace(/&nbsp/g','');

// Set the plain text content to the Journal field
g_form.setValue('journal_field', plainTextContentis);

View solution in original post

Thank you Jaspal,

 

After removing HTML Tags and &nbsp  we recieved ; semicolon which we replaced and then got the Correct Solution with below script.

 

var itgj = g_form.getValue('html_field');
 
// Remove HTML tags using a regular expression
var plainTextContent = itgj.replace(/<[^>]*>/g, '');
 
//remove &nbsp
var plainTextContentis=plainTextContent.replace(/&nbsp/g,'');
 
var plainTextContent1 = plainTextContentis.replace(/;/g, '');
 
// Set the plain text content to the Journal field
g_form.setValue('journal_field', plainTextContent1);

View solution in original post

5 REPLIES 5

jsilvey
Tera Contributor

Hey All,

 

Can someone help me understand why this does not remove the &nbsp? 

// Remove HTML tags using a regular expression
var plainTextContent = htmlFieldValue.replace(/<[^>]*>/g,'');

//remove &nbsp
plainTextContent = plainTextContent.replace(/&nbsp/g','');

 

Why do we need to declare a new variable in the second statement? Why aren't we able to simply reuse the first variable and store a new value in it that removes the &nbsp?

 

Thank you!