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 g flag, returns same result regexp.exec(string). if regular expression includes g flag, method returns array containing matches. if there no matches, method returns null.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -