Skip to page contentSkip to chat
ServiceNow support
    • Community
      Ask questions, give advice, and connect with fellow ServiceNow professionals.
      Developer
      Build, test, and deploy applications
      Documentation
      Find detailed information about ServiceNow products, apps, features, and releases.
      Impact
      Accelerate ROI and amplify your expertise.
      Learning
      Build skills with instructor-led and online training.
      Partner
      Grow your business with promotions, news, and marketing tools
      ServiceNow
      Learn about ServiceNow products & solutions.
      Store
      Download certified apps and integrations that complement ServiceNow.
      Support
      Manage your instances, access self-help, and get technical support.
ServiceNow JavaScript escape characters - Support and Troubleshooting
  • >
  • Knowledge Base
  • >
  • Support and Troubleshooting (Knowledge Base)
  • >
  • ServiceNow JavaScript escape characters
KB0721958

ServiceNow JavaScript escape characters


22194 Views Last updated : Mar 16, 2023 public Copy Permalink English (Original)
  • English (Original)
  • Japanese
KB Summary by Now Assist

Issue

Certain characters, if used in a JavaScript string, can generate unexpected and sometimes hard to identify errors in a script.  These Javascript strings can be used within various object types that support scripting in the ServiceNow platform, such as Business Rules, Workflow Scripts, ACL records and many other places in the application.  These special characters generally cause an issue because that character has a special significance to the JavaScript language when included in a command.

To allow these characters to thus be used in a String when necessary in the Javascript supported objects in ServiceNow, the character must be escaped (preceded by special characters telling the JavaScript compiler to handle the next character literally rather than attempt to interpret the character in the usual manner).

Method

The three main characters that users will probably need to use and which will require the escape character are the single quote character ('), the double quote character (") and the backslash character (\).

The reason these characters need special handling is because the single and double quote characters are used to begin and terminate a string in JavaScript and the backslash character is the escape character itself.

Escaping these characters is easy, simply place a preceding backlash character (\) in front of the character within the string which you want to have treated literally.  Thus, \' would allow usage of a single quote character, \" would allow usage of the double quote character and \\ would allow literal use of a backslash character in a string.

Example

As an example, the following two JavaScript statements will each generate an error due to of these special characters included within a string:

var userName = "Jerome "The Race" Anderson";

This statement would produce the following error message output:

Javascript compiler exception: missing ; before statement (null.null.script; line 1) in:
var userName = "Jerome "The Race" Anderson";

And, the following JavaScript statement, containing a special character in the string:

gs.log('Please retry the operation, the expected button wasn't found on the form.');

would generate the following error due to the special character found in the string:

Javascript compiler exception: missing ) after argument list (null.null.script; line 1) in:
gs.log('Please retry the operation, the expected button wasn't found on the form.'); 

The following statement would not necessarily generate an error, however, it would most probably not produce the expected results.

var filepath = "\\bzavr\extra\open\files\config.ini";

 The filepath variable, as stored in memory would actually contain the following string text:

\bzavrextraopenilesconfig.ini

To rectify these issues and allow these statements to work correctly, we would need to precede each of the special characters in the strings with the Javascript backslash escape character.

Thus, the previous statements could be re-written as the following to prevent any issues which might be introduced by including these special characters in a string:

var userName = "Jerome \"The Race\" Anderson";
gs.log('Please retry the operation, the expected button wasn\'t found on the form.');
var filepath = "\\\\bzavr\\extra\\open\\files\\config.ini";

Additional Information

 

Note also that the use of the backslash character can also be used to allow the inclusion of special, non-printable characters within a Javascript string.  The following shows the character combination and the non-printable character represented:

Escaped CharacterCharacter Representation 
 \b Backspace
 \f Form Feed
 \n New Line
 \r Carriage Return
 \t Horizontal Tab
 \v Vertical Tab

Thus, to include any of these non-printable characters in a Javascript string, the escaped character should be added (i.e. \n to include a new line).

For example the following statement,

var textData = "This is a test string.\n\rNext Line.\tShifted line.";

when printed, might show output similar to the following:

This is a test string.
Next Line.	Shifted line.

The world works with ServiceNow.

Sign in for more! There's more content available only to authenticated users Sign in for more!
Did this KB article help you?
Did this KB article help you?

How would you rate your Now Support digital experience?

*

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

What can we improve? Please select all that apply.

What are we doing well? Please select all that apply.

Tell us more

*

Do you expect a response from this feedback?

  • Terms and conditions
  • Privacy statement
  • GDPR
  • Cookie policy
  • © 2025 ServiceNow. All rights reserved.