Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Evaualte a String containg new line characters.. 2

Status
Not open for further replies.

portalguy123

Programmer
Sep 19, 2006
40
US
Hello Experts,

I have a scenario where I need to eval a string which contains newline characters..The string result2 has the following script as its value, however when I evaluate the string with eval(result2); it seems it does not get evaluated ..Please let me know if there is a workaround for this problem.

Thanks a lot!

result2=
Code:
// Build the netui_names table to map the tagId attributes
// to the real id written into the HTML
if (netui_names == null)
   var netui_names = new Object();
netui_names.fname0="fname0"
netui_names.email0="email0"
netui_names.lname0="lname0"
netui_names.box0="wlw-checkbox_key:{request.results[0].checked}"
netui_names.mi0="mi0"
 
Please let me know if there is a workaround for this problem.

Maybe try taking out the comments? If you're running an eval on the string I don't know why you'd need to have the comments. If you're dead-set on leaving the comments in, then try using the alternate form of /* comment here */ - that way the eval won't have to worry about line breaks because it will know where the comment ends.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
This script is auto generated from the portal framework, and is part of teh ajax response which gets populated in the innerHTML of a div element, I need to eval it so that the values it puts the mapping object reflect globally.And thats why I was trying to eval it.
 
Hi cLFlava,

What is the the workaround for this other than using eval?
 
Hello Kaht,

No, I cant remove it becuase that script is being auto generated.
 
The string is autogenerated, and then stored in a string before you eval it, right? You'll have full control over that string before it's eval'd. Just remove the comments before it's eval'd:
Code:
<script type="text/javascript">
var a = [COLOR=black yellow]'// Build the netui_names table to map the tagId attributes\n // to the real id written into the HTML\n if (netui_names == null)\n   var netui_names = new Object();\n netui_names.fname0="fname0"\n netui_names.email0="email0"\n netui_names.lname0="lname0"\n netui_names.box0="wlw-checkbox_key:{request.results[0].checked}"\nnetui_names.mi0="mi0"\n';[/color]


alert("String before replace:\n\n" + a);


a = a.replace(/\/\/.*\n/g, "");
alert("String after replace:\n\n" + a);


//eval(a);
</script>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
The question is not described what really appeared, I would say.

[1] To the letter of the op's first post, this shows you there is not really a problem of eval() which is calling the compiler no different from the normal course of compiling. (I put a blank space surrounding \n to avoid wide-post to the forum!)
[tt]
result2='// Build the netui_names table to map the tagId attributes[highlight] \n [/highlight]// to the real id written into the HTML[highlight] \n [/highlight]if (netui_names == null)[highlight] \n [/highlight] var netui_names = new Object();[highlight] \n [/highlight]netui_names.fname0="fname0"[highlight] \n [/highlight]netui_names.email0="email0"[highlight] \n [/highlight]netui_names.lname0="lname0"[highlight] \n [/highlight]netui_names.box0="wlw-checkbox_key:{request.results[0].checked}"[highlight] \n [/highlight]netui_names.mi0="mi0"';

eval(result2);
alert (netui_names.box0);
[/tt]
[2] Now, if it is revealed it really is innerHTML of some div... chances are the result2 is html encoded!!! Let me distribute all sorts of plausible encoded version (good html or bad: <br /> or <br/> or <BR/> etc, or <p> with </p> or <p> without </p>; and some &nbsp; etc). (Again I add a blank space surrounding them to avoid wide-post to the forum.)
[tt]
result2='// Build the netui_names table to map the tagId attributes <br /> // to the real id written into the HTML <p> if (netui_names == null) </p> <p> &nbsp;&nbsp;&nbsp;var netui_names = new Object(); </p> <br/> netui_names.fname0="fname0" <br/> netui_names.email0="email0" <br/> netui_names.lname0="lname0" <BR/> netui_names.box0="wlw-checkbox_key:{request.results[0].checked}" <br/> netui_names.mi0="mi0"';

//the transformations before eval
result2=result2.replace(/<br.*?>|<p>/gi,"\n");
result2=result2.replace(/<\/p>/gi,"");
result2=result2.replace(/&nbsp;/gi," ");

eval(result2);
alert (netui_names.box0);
[/tt]

[1] & [2] should reveal netui_names.box(0) be "wlw...." as expected.
 
Thank you kaht and tsuji,

I was trying kaht's suggestion first and it seems the regexp does not replace the comments the string is as it is after the replace with no changes..can you please look into that..
I am using the following code
Code:
   result2.replace(/\/\/.*\n/g, "");
   alert ('Result 2 after replacing is '+result2);

Tsuji,

When the alert the result2 string which has the script stored in it , its does not show any HTML tags in the alert box, is it possible for the html tags to be ignored in the alert dialog box..please let me know..
 
can you please look into that..

Umm...... the example I pasted above worked perfectly fine if you copy/paste it into a new html file. If it didn't work for you it's not because the regexp is broken, but because your string didn't fit the criteria for the regexp. I built the string "a" above with the code you supplied in an above post. If the string being pulled in differs from this, then how would I know how to build the regexp to compensate for that? As tsuji suggests above, maybe your string holds <br> tags instead of newline characters. As such, I wrote the regexp to compensate for newline characters, if this isn't what your string holds then my solution obviously isn't going to work. However, it's impossible for me to "look into it" if I don't know the specs of the problem....

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Thanks Kaht,

You were right the eval was not working becuase of the comments , I have removed the comments using split method becuase I know that the first two lines will always be comments , and after stripping those comments eval works like a charm.And no the script does not contain any html tags ..

How did you know that the comments were causing the problem, with java script I have always found it to be a problem to find good reference material , any book or resource you can suggest..

Thanks Guys, I really appreciate yoru help..

-Bob F
 
>eval was not working becuase of the comments
Are you all really sure?! so simple to verify and does it need me to offend people!
 
You said that it was acting like it was ignoring the newline characters, that being the case your code would have looked like this:

Code:
// Build the netui_names......netui_names.mi0="mi0"

If it ignores the newline characters then it looks like your code would be one big comment line. That's why I suggested removing the comments or using /* */ instead. Had you used the other method of comments it would be like this:
Code:
/* Build the netui_names */ ...... netui_names.mi0="mi0"
And the eval would have an indicator of where the comment stopped - instead of assuming the whole line was a comment.


As far as reference material, google and tek-tips are all that I use. If you're a beginner you might wanna buy a beginner's book to javascript and read it from cover to cover to learn the basics, but coding in it every day is the best thing you can do to expand your skills using it.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
OK Tsuji,

Interesting...I can confirm it was not becuase of the comments but becuase of the if statement, now that I am ignoring the if statment it works fine I ignore it becuase the condition that if statement checks is always false and teh object it checks for always exists, but it seems eval is ignoring all the statements after the if statements if the statement evaluates to false,I mean the statements after the first semicolon too..Again my string hasnt chnaged its the same..

result2=
Code:
// Build the netui_names table to map the tagId attributes
// to the real id written into the HTML
if (netui_names == null)
   var netui_names = new Object();
netui_names.fname0="fname0"
netui_names.email0="email0"
netui_names.lname0="lname0"
netui_names.box0="wlw-checkbox_key:{request.results[0].checked}"
netui_names.mi0="mi0"
 
Try putting semicolons after each statement (I know they're not typically necessary, but this is different than "typical") and enclose your conditional code (inside the if statement) in curly braces, if you keep it.

Lee
 
Hello Lee,

The problem is that its autogenrated, I will have to keep doing complex string manipulations using complex RegExps..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top