javascript - Returning true for strings that begin with a hashtag using regex -
i'm confused why in http://regexr.com/3bcns, ^#
isn't matching #hello-link
, string begins hashtag.
this want do:
if ((/^\.\./).test(link)) { alert('link starts ..'); } else if (!(/#/).test(link)) { alert('does not contain #'); } else if ((/^#/).test(link)) { alert('link begins #'); }
also, i'm curious: there reason why people prefer use .test
instead of .match
when using regexes in javascript?
why not check first character of string?
function start_with_hash(str){ return str[0]=='#'; }
Comments
Post a Comment