Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Issue in unterminated regular expression literal with eval() function

New Here ,
Aug 26, 2021 Aug 26, 2021

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.

 

 

 

TOPICS
JavaScript , PDF forms
3.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 26, 2021 Aug 26, 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.

 

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 26, 2021 Aug 26, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 27, 2021 Aug 27, 2021

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 27, 2021 Aug 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")) ;

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 27, 2021 Aug 27, 2021

It can be used to do it, for example:

 

eval("var data = {a:{name:'abc'}}");
app.alert(data.a.name);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 27, 2021 Aug 27, 2021

Hello,

 

Thank you for your reply. Actually I got some really good guidance here in this: https://www.pdfscripting.com/public/FreeStuff/PDFSamples/PopulateFieldsFromXML_Sample.pdf

but when I tried to access the child nodes, excerpt from the link above: 

//Check to see if it is the correct XML Gramar
var nLen = xmlData.child("Customer").length();

TypeError: xmlData.child is not a function

 

Can you help me access the child nodes and children based from the example on the link.

That would be really helpful.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 27, 2021 Aug 27, 2021

Review the instructions on how to install this folder level script:

https://www.pdfscripting.com/public/Installing_Automation_Tools.cfm?sd=40

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 28, 2021 Aug 28, 2021

Hmmm. I am not on top of "eval" but, I don't understand what it's doing in 

eval("var data = {a:{name:'abc'}}");
app.alert(data.a.name);

 

Surely this works well without the eval

var data = {a:{name:'abc'}}
app.alert(data.a.name);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 28, 2021 Aug 28, 2021
LATEST

LOL... You're right, of course.

But imagine if the data comes in the form of a string from an external source, like a .json or .xml file. You can use eval to convert it into a variable that can be used in the rest of the code with a single command, instead of having to parse it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines