0

I am using the following script but it's giving syntax errors which I am unable to figure out. Please help.

var str = "http://gaurav.com";

var patt1 = /^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$/;
console.log(str.match(patt1));

Thanks, Gaurav

0

5 Answers 5

2

Needs to be in /'s

var patt1 = /^http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/;

Edit: also escape /s in the pattern.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks for pointing that out... but it's yet not working.
I tried it in Firebug, there's some kind of syntax error still, maybe unescaped regex meta character?
1

You pattern gives a "," in the string, could that be the problem???

Try this:

var str = "http://gaurav.com";
var patt1 = 'http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}';
console.log(str.match(patt1));

See the working example here

Comments

0

You must escape all / as well:

var str = "http://gaurav.com";
var patt1 = /^http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/;
//------------------^-^---------------------------------^
console.log(str.match(patt1));

Comments

0

Quote the regexp string, that should fix the error. Haven't checked the expression, but that wasn't the question, right?

var patt1 = '^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$';

Comments

0

This seems to work just fine - looks like you're just missing your quotes

var str = "http://gaurav.com";

var patt1 = "^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$";
document.write(str.match(patt1));

Here's a jsfiddle link to the code you can play with http://jsfiddle.net/chuckplayer/fLrx8/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.