Javascript regex to find a base URL -
i'm going mad regex in js:
var patt1=/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*?(:[0-9]+)?(\/)?$/i;
if give input string "http://www.eitb.com/servicios/concursos/516522/" regex it's supossed return null, because there "folder" after base url. works in php, not in javascript, in script:
<script type="text/javascript"> var str="http://www.eitb.com/servicios/concursos/516522/"; var patt1=/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*?(:[0-9]+)?(\/)?$/i; document.write(str.match(patt1)); </script>
it returns
http://www.eitb.com/servicios/concursos/516522/,,/516522,,/
the question is: why not working? how make work?
the idea implement regex in function null when url passed not in correct format:
http://www.eitb.com/ -> correct http://www.eitb.com/something -> incorrect
thanks
i'm no javascript pro, accustomed perl regexp, i'll give try; .
in middle of regexp might need escaped, can map /
, jinx whole regexp.
try way:
var patt1=/^http(s)?:\/\/[a-z0-9-]+(\.[a-z0-9-]+)*?(:[0-9]+)?(\/)?$/i;
Comments
Post a Comment