problem with html entity in string

  • Thread starter Juan R. González-Álvarez
  • Start date
J

Juan R. González-Álvarez

I want to substitute "\(" by "("

i have tried

content = content.replace(/\\\(/g, "(");

content = content.replace(/\\\(/g, unescape("%26") + "#40;");

content = content.replace(/\\\(/g, "\&" + "#40;");


In all three cases the resulting HTML is "("

How do i force "&" to be "&" instead "&"?
 
H

Henry

I want to substitute "\(" by "("

i have tried

content = content.replace(/\\\(/g, "(");

content = content.replace(/\\\(/g, unescape("%26") + "#40;");

content = content.replace(/\\\(/g, "\&" + "#40;");

In all three cases the resulting HTML is "("

The result of the javascript - replace - method is a javascript string
primitive, not HTML. If the input string is the backslash character
followed by the opening parenthesis character then the output string
is _never_ "(". So the odds are that either you have omitted
context that is significant to your problem of your problem is down-
stream of your use of the - replace - method (or possibly your input
strings are not what you think they are).
How do i force "&" to be "&" instead "&"?

There is nothing to force until the cause and effect relationship has
been identified, and it is not in the - replace - method calls. Try
posting a minimal text-case page that demonstrates the effects you
observe in isolation.
 
J

Juan R. González-Álvarez

Henry wrote on Thu, 08 May 2008 06:38:38 -0700:
The result of the javascript - replace - method is a javascript string
primitive, not HTML.

Exactly i want to substitute a string by other.
If the input string is the backslash character
followed by the opening parenthesis character then the output string is
_never_ "(".

But i got just that output in end page. Is then the problem in the
createTextNode method (see below)?
So the odds are that either you have omitted
context that is significant to your problem of your problem is down-
stream of your use of the - replace - method (or possibly your input
strings are not what you think they are).

E.g. the input string is "\(n\)"

and the result i got is "(n)"

but it would be "(n)"
There is nothing to force until the cause and effect relationship has
been identified, and it is not in the - replace - method calls. Try
posting a minimal text-case page that demonstrates the effects you
observe in isolation.

Ok, try with this to see the script in action

http://www.canonicalscience.org/en/researchzone/canonical.html

The code is at

http://www.canonicalscience.org/en/codes/main.js

the part is at the end /* CANONML TO HTML */
 
J

Juan R. González-Álvarez

"Juan R." González-Ãlvarez wrote on Thu, 08 May 2008 16:51:29 +0200:
Henry wrote on Thu, 08 May 2008 06:38:38 -0700:


Exactly i want to substitute a string by other.


But i got just that output in end page. Is then the problem in the
createTextNode method (see below)?

Ok i think that Henry is right and the problem is not in replace but in
the posterior createTextNode method when it finds the "&" in the string
and escape like "&" when generating the HTML.

What would be more easy way to fix that?
 
T

Thomas 'PointedEars' Lahn

Juan said:
Ok i think that Henry is right and the problem is not in replace but in
the posterior createTextNode method when it finds the "&" in the string
and escape like "&" when generating the HTML.

What would be more easy way to fix that?

ISTM there is no problem but a misconception of yours that needs fixing.
The DOM2Core:Document::createTextNode() method that you presumably speak
of takes a DOMString value as argument, supported by a primitive string
value in ECMAScript implementations per the DOM 2 Core Specifications'
ECMAScript Binding section.

document.createTextNode("&")

is supposed to create a TextNode object[1] with the content

&

and return a reference to it which is represented in HTML by

&

among other entity references. Maybe you think that

<a href="http://foo.example/?bar=42&amp;baz=23">...</a>

would not be correct in which case you should read on SGML and HTML, and
validate your markup (you should do that anyway) because you were wrong then.

See also:

http://www.w3.org/TR/DOM-Level-2-Core/
http://www.w3.org/TR/html4/
http://validator.w3.org/


PointedEars
 
J

Juan R. González-Álvarez

"Juan R." González-Ãlvarez wrote on Thu, 08 May 2008 14:44:59 +0200:
I want to substitute "\(" by "("

i have tried

content = content.replace(/\\\(/g, "(");

content = content.replace(/\\\(/g, unescape("%26") + "#40;");

content = content.replace(/\\\(/g, "\&" + "#40;");


In all three cases the resulting HTML is "&amp;#40;"

How do i force "&" to be "&" instead "&amp;"?

Yes, the problem was on the CreateTextNode and not in the replace method.
I had simply adapted a previous code for CanonML 0.6 to CanonML 0.7 and
then introduced replace.

I have eliminated replace method introduced a next token variable and
modified the nested if to go over escaping backslashes. It is now
working. See

http://www.canonicalscience.org/en/researchzone/canonical.html

Place your mouse over an equation and you can see original code.

Thanks Henry and Thomas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top