Skip to main content
Participant
January 28, 2018
Question

Newbie question re replace in string

  • January 28, 2018
  • 2 replies
  • 2101 views

I created an "action", to get a document's file and path by copying the following code (which I found somewhere on the web) into the console:

/* Put script title here */

console.show(); // open console

console.clear(); // clear contents

console.println(this.path); // display the current pdf's path

The action generates the following text in the console output window:

/F/VB/PythonAndPySide_Info/Python_Info/Introduction to Python Classes (Part 1 of 2) - Python Central.pdf

I am beginning to try to learn javascript.  (I'm comfortable in vba and python, but I'm having trouble learning Acrobat javascript).

I want to use javascript to (1) insert a ":" after the drive letter "F";  (2) remove the first forward slash ("/");  and (3) replace the replace the remaining forward slashes with back slashes.

I tried unsuccessfully to do #3 first, but the following code produced an error message:

/* Put script title here */

console.show(); // open console

console.clear(); // clear contents

var fullname = this.path;

fullname = fullname.replace("/", "\");

console.println(fullname); // display the current pdf's path

The error message was:

SyntaxError: unterminated string literal

5: at line 6

I would very much appreciate any help anyone could give me.    I don't know where on the web I could learn this stuff.

                      Marc

This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
January 28, 2018
Inspiring
January 28, 2018

The JavaScript Escape character is "\". This character is used to signal JavaScript to create characters that you cannot in JavaScript like control characters (new line, return, tab, etc.) or that the next character is to be treated as a part of the string and not a terminating string character. Sine the "\" is a special character one needs to prefix it with a "\" to have the string value include the "\" as a character in the string.

You should be getting an error message in the JavaScript console indicating the the line in the JavaScript with the error.

/* Put script title here */

console.show(); // open console;

console.clear(); // clear contents;

var fullname = this.path;

fullname = fullname.replace("/", "\\");

console.println(fullname); // display the current pdf's path;

This will only replace the first occurrence of the "\". To replace the "\" globally you need to use the RegExp with the global flag.

marceepooAuthor
Participant
January 29, 2018

Thank you.  I'll read the pages to which Thom Parker referred me, one of which I think shows how regular expressions are used in Javascript (https://acrobatusers.com/tutorials/text-matching-regular-expressions)

1.  Thank you for telling me:

gkaiseril  wrote

This will only replace the first occurrence of the "\". To replace the "\" globally you need to use the RegExp with the global flag.

That does not explain why Acrobat gives me this error message:

SyntaxError: unterminated string literal

5: at line 6

If one replacement was made by my code, why do I get an error message?

2.  I think there's a typo in the line "You should be getting an error message in the JavaScript console indicating the the line in the JavaScript with the error." i.e., I don't understand what you are saying (and I apologize for being obtuse).

3.  Could I impose on you to show me how to perform this replace?  Of if not, can you help me find some examples of javascript (in an acrobat context) where a regular expression replace is performed?  I know how to do these things in vba and python.  I will read the links that Thom Parker gave me.

Bernd Alheit
Community Expert
Community Expert
January 29, 2018

1. Use "\\" not "\"