Skip to main content
Participant
August 27, 2021
Question

Issue in unterminated regular expression literal with eval() function

  • August 27, 2021
  • 2 replies
  • 4422 views

Hi,

 

I am creating a fillable form on Acrobat Pro DC (acroform) and I would like to parse my xml but in this line 

var strData = util.stringFromStream(myXMLFile);

var xmlData = eval(strData.replace(/^\<\?.*\?\>\s*/,""));

I am getting

"SyntaxError: unterminated regular expression literal
182:Document-Level:PopulateDropdown"

 

it seems the eval() is causing the error but if I remove eval() I will have an issue in this line:

var nLen = xmlData.child("Branch").length();

I am getting this: 

TypeError: xmlData.child is not a function
13:Document-Level:PopulateDropdown

 

Any help will be much appreciated.

 

Thanks.

 

 

 

This topic has been closed for replies.

2 replies

Legend
August 27, 2021

Why do you wrap strdata.replace(...) in eval at all? Is the result of the replace a valid JavaScript statement or expression? What do you see the purpose of eval as?

 

also what (informally) are you trying to achieve with the replace? Are you really trying to delete from the start of each line constructs like 

<?anything anything?>anything...<?anything?> followed by spaces, but not at all if not followed by spaces?

try67
Community Expert
Community Expert
August 27, 2021

My guess is they want to convert that string into a literal object, to be used further in the script.

Legend
August 27, 2021

But that's not what eval does (convert to a literal object)... or is it...? To my mind, it executes a JavaScript command, and has no other purpose...

var string="app.alxrt(\"hxllo world\")";
eval(string.replace(/x/,"e")) ;

 

ls_rbls
Community Expert
Community Expert
August 27, 2021

I don't know JavScript very well and I am still learning, but I couldn't help but notice the following (please excuse if my observations are too rookie):

 

I am unsure why these variables are declared like this:

 

1)   In the  line below, it seems like a variable is missing a replacement function to search for the desired string (but I may be wrong)

 

  • var strData = util.stringFromStream(myXMLFile);

 

You may have come accross this article by ACP Thom Parker: Text matching with regular expressions using JavaScript 

 

Also see the example on Page 20 of the Acrobat JS Developer Guide , "Debugging with the JavaScript Console".

 

2)   And in this line what is the use of  ,""  at the end ?

  • var xmlData = eval(strData.replace(/^\<\?.*\?\>\s*/,""));

 

See also JavaScript™ for Acrobat® API Reference util methods for the following:

 

  • getDataObjectContents
    streamFromString
    stringFromStream

 

 

3-)   Last, this variable:

 

  • var nLen = xmlData.child("Branch").length();

 

Shouldn't it be: var nLen = xmlData.child("Branch").length;  without the "()" ?

 

Again, please excuse if my observations are not up to par with your JavaScript knowledge or the experts of these support forums.