javascript - Regular expression returning some awkward value -
i have 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 expected to find tags in html document. ideally should return <a id="test">hi</a>, <p></p>.
but returns <a id="test">hi</a>, ,hi.
why happening?
also question, what difference between str.match(patt) , patt.exec(str) , better use?
var patt=/[<](\s+).*>(.*)<\/\1>/g; try specify global modifier (or stop @ first occurrence found).
about second question mdn resource:
https://developer.mozilla.org/en/javascript/reference/global_objects/string/match
if regular expression not include
gflag, returns same result regexp.exec(string). if regular expression includesgflag, method returns array containing matches. if there no matches, method returns null.
Comments
Post a Comment