I have the following javascript code:
<script type="text/javascript"> //javascript starts
var patt=/[<](\S+).*>(.*)<\/\1>/;
var str='<a id="test">hi</a> <p></p>';
alert(str.match(patt));
alert(patt.exec(str));
</script>
It is expected to find all tags in a html document. So ideally it should return <a id="test">hi</a>, <p></p>.
But it currently returns <a id="test">hi</a>, a ,hi.
Why is this happening?
Also another question, What is the difference between str.match(patt) and patt.exec(str) and which is better to use?
try to specify the
globalmodifier (or it will stop at the first occurrence found).About your second question MDN is a good resource:
From https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/match