Karthik said:
MOdified the pattern to
var patt="(<" + tagname + ">
" + "(*)" + "(<." + tagname +
">
";
without the intial / and ending /g still no go...
Hi All,
I am trying to match an XML tag using JS regular expressions. The
pattern I am using is
pattern="/(<" + tagname + ">
" + "(*)" + "(<." + tagname +
">/g)";
where I want to replace the tagname variable with the name of the tag
which I want to search for. Unfortunately this doesn't work. If I
replace the tagname variable with the actual tag's name it works.
Any idea how to fix this issue?
If any of you could post a script that could do this it would be
great.
[...]
Got the expression...
Not at all, you don't.
here it is...
var regexpr= new RegExp("(<" + tagname + ">
([A-Z]*[[a-z]*[0-9]*)
(<." + tagname + ">
");
apply a exec of this pattern on any string/html source/xml file, it
will fetch you the values between the tags..
Only if the content is ASCII-alphanumeric. XML, however, is UTF-8-safe.
one word of warning though if the tag has got child tags, it will ^^^^^^^^^^^^^^^^^^^^^^^^^^
retrieve all the child tags also
^^^^^^^^^^
http://www.w3.org/TR/REC-html40/intro/sgmltut.html#h-3.2.1 (esp. the last,
green-colored paragraph)
It will _not_ match any child _elements_, as you have explicitly excluded
their start tags from the content of the `tagname' element, assuming that
the double `[' was but a typo (if it was not, the expression would match `['
in the content as well). Why you escape `<' and `>' remains a mystery;
further assuming that you use it within an XHTML `script' element (where
declaring it as CDATA would have sufficed to avoid the character entity
references), the possible match would be
<foo>abc<bar>def</bar>ghi</foo>
^^^^^^^^^^
However, that match is discarded because `ar' does not match `fo'.
The Chomsky hierarchy, taught in computer science classes, tells us that
it is usually not possible to use (only) a regular grammar, such as the one
regular expressions are based on, to parse a context-free language, such as
SGML-based markup. Because every regular language is context-free, but not
every context-free language is regular.
Therefore, only if you need to parse the markup as such instead of accessing
the corresponding DOM objects, you are looking for a non-deterministic
pushdown automaton (which can parse those languages), implemented as an XML
parser (such as DOMParser in Gecko-based UAs), instead. If you don't want
to use such an external API, it is possible to combine the efficiency of
regular expression matching with the reliability of an NPDA in your code.
http://en.wikipedia.org/wiki/Chomsky_hierarchy
PointedEars