regEx for HTML element

S

sri_san

Hello,
I am trying to parse a HTML page and want to replace the input
element

The following code fails all the time.

var ex = "<input type=\"hidden\" name=\"__VIEWSTATE\"
id=\"__VIEWSTATE\"
value=\"/wEPDwULLTE2NjEyNTI0MThkGAEFEHNlY3Rpb25zR3JpZFZpZXcPZ2QN271==
/>";
var regEx = new RegExp("<\s*input[^>]*>(.*?)\s*/> ");
if (ex.match( regEx))
{
alert('match');
}
else
{
alert ('no match');
}


any help would be great!

Thanks,
Srini.
 
E

Evertjan.

wrote on 03 jan 2007 in comp.lang.javascript:
Hello,
I am trying to parse a HTML page and want to replace the input
element

The following code fails all the time.

var ex = "<input type=\"hidden\" name=\"__VIEWSTATE\"
id=\"__VIEWSTATE\"
value=\"/wEPDwULLTE2NjEyNTI0MThkGAEFEHNlY3Rpb25zR3JpZFZpZXcPZ2QN271==
/>";
var regEx = new RegExp("<\s*input[^>]*>(.*?)\s*/> ");
if (ex.match( regEx))
{
alert('match');
}
else
{
alert ('no match');
}

For replacing I would use replace() not match():


<div id='container'>
======== other things ========
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwULLTE2NjEyNTI0MThkGAEFEHNlY3Rpb25zR3JpZFZpZXcPZ2QN271" />
======== other things ========
</div>

<script type='text/javascript'>
var s = document.getElementById('container')

s.innerHTML =
s.innerHTML.replace(/<input ([^>]*)>/gi,'<br>Replacing ...<br>(arguments:
$1)<br>')

</script>
 

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
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top