Issue with text.replace

Ian Mildon
Tera Guru

I am using a section of script added to a Script Include to convert the contents of an HTML field to a text field; to remove all HTML encoding characters. This is working really well, with one exception.

Anytime there is an apostrophe in the HTML text it is being converted to HTML in the text field. So, in the final text, instead of seeing an ' I'm getting '

Here is the script I am using to convert the text:

var text = demand.getValue("business_case").toString();
var regX = /(<([^>]+)>)/ig;
var finalText = text.replace(regX, " ");

project.setValue("description", finalText);

 

How best can I further modify this to insure any apostrophe is correctly rendered?

4 REPLIES 4

Chuck Tomasi
Tera Patron

I had some similar exceptions with &nbsp; and such when importing info from Wikipedia and stripping out the HTML.

 

I generally address escaped/unicode characters like that with special cases one at a time (it makes it quite readable and maintainable.)

 

E.g.

var text1 = text.replace("&#39:", "'");

 

FWIW, you don't need a toString() on the end of that first line. getValue() of a string field is going to return a string. You're effectively saying "turn this string in to a string." 🙂

Anurag Tripathi
Mega Patron
Mega Patron

What fi you do this

 

var text = demand.getValue("business_case").toString();

var regX = /(<([^>]+)>)/ig;

var finalText = text.replace(regX, " ");

finalText = finalText.replace('&#39;', "'");

project.setValue("description", finalText);

-Anurag

Ian Mildon
Tera Guru

I ended up taking both of your replies and updating my script to the following:

var text = demand.getValue("business_case");
var regX = /(<([^>]+)>)/ig;
var finalText = text.replace(regX, " ");

finalText = finalText.replace("&#39;", "'");
project.setValue("description", finalText);

Which now works well and no more weird characters.

But, it is replacing at first time, if it is repeated , it's not.

is there any fix to replace for multiple times on the special character in the description like "@", "+"