cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

backslashes in the text of a field

ptc-4668818
1-Newbie

backslashes in the text of a field

It seems that PTC Integrity does not handle the backslash character as a special character when it appears within the text of a field. This becomes a problem for report recipes that utilizes JavaScript. For example, the following text will be fine for an HTML portion of a recipe, but will not work if brought in to a JS variable:

C:\test\uninstela\seaf\nerasl

If this text was contained in a test annotation, for example, and the text was assigned to a JS variable in the recipe like this:

var test_annotation = "<%testresult Annotation %>";

the resulting script at runtime would be:

var test_annotation = "C:\test\uninstela\seaf\nerasl";

the browser would produce an error because it is expecting a hex value after the \u in this string.

Why doesn't the script generator handle this? It should convert the backslash to &#92; (or something like this) before sending it to the browser:

var test_annotation = "C:&#92;test&#92;uninstela&#92;seaf&#92;nerasl";

Has anyone else run into this problem? If so, have you found a workaround?

2 REPLIES 2

The backslash is an escape character in javascript, so it has to be escaped (ironically, by a backslash). For example, like this:
"C:\test\folder\subfolder" needs to be "C:\\test\\folder\\subfolder" to not break in javascript.

A quick little function that I use to automatically escape special characters in javascript is:
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

When setting your variable, use this function to properly escape any problem characters. For example:


var test_annotation = escapeRegExp("<%testresult Annotation %>");

This is the equivelent of the PHP funciton addslashes()

The problem occurs when using an Integrity report recipe where Integrity populates a field in the script that gets assigned to a variable in a javascript segment. I have found no way to be able to fix the slashes in the string before javascript strips out what it thinks are escape sequences. For example, I have a portion of the recipe that looks something like this:

<script language='JavaScript'>
<%beginrelationshipsdetail My_relationship_field%>

myvariable = '<%Relationship fieldnameofinterest%>';

<%endrelationshipsdetail%>

</script>

If the content in the field name of interest contains a backslash, like in a pathname, there will be missing characters in myvariable. The most severe situtation I found was when the character following the backslash is a 'u'. This escape sequence expects to be followed by a hex value format and, if not present, will immediately cause a script error when encountered.

Top Tags