regex - JavaScript match function seems to halt script -
regex - JavaScript match function seems to halt script -
yesterday 'hnatt' kind plenty offer regex portion of script :
<html> <body> <script type="text/javascript"> alert("hhhhhh yadad example.com/check?x=asdfasdf bsss ffhhh".match(/example.com\/check\?x\=([^\s]*)/)[1]); alert('alert 2'); </script> </body> </html> now have new question/problem/point_of_confusion. if alter 'example.com' not match, entire script stops. know solution other try/catch permits script go on forward. (although, hacked prepare try/catch, insertion of seek catch/breaks larger script... don't know why. why want solution not comprised of try/catch). seek understand why halsting happens when 'match' function not find match.
<html> <body> <script type="text/javascript"> alert("hhhhhh yadad exampletwo.com/check?x=asdfasdf bsss ffhhh".match(/example.com\/check\?x\=([^\s]*)/)[1]); alert('alert 2'); </script> </body> </html> this boiled downwards version. in broader script using the needle found in haystack , assigning variable.
if want not generate script error , not utilize try/catch, need assign match homecoming value variable , test see if found match , if found plenty matches before using it.
var matches = "hhhhhh yadad exampletwo.com/check?x=asdfasdf bsss ffhhh".match(/example.com\/check\?x\=([^\s]*)/); if (matches && matches.length > 1) { alert(matches[1]); } javascript regex
Comments
Post a Comment