0

Regex is my weak skills. How to check by regex following strings:

http://www.facebook.com/johnsmith
http://www.facebook.com/profile.php?id=100000111111111
http://www.facebook.com/people/John-Smith/100000111111111

so, only similar strings should be valid, rest are invalid. How to do it?

1
  • 1
    what do you mean by similar strings? Commented Aug 11, 2011 at 15:36

3 Answers 3

1

EDIT

I think I see what you mean.

var url = "http://www.facebook.com/profile.php?id=100000111111111";
(/^http:[/][/]www[.]facebook[.]com[/]((profile[.]php[?]id=[0-9]{15}|people).*$|[A-za-z]*$)/).test(url);

would something like that work?

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

3 Comments

thank you. But it says that string facebook.com/profile.php?id=1XXX is correct. How to fix
@user285336 isn't it supposed to? I'll amend it... is the id always of the format 100000111111111?
I mean if I enter facebook.com/profile.php?id=1USER your regex says that I enter correct string
0

Start with a simple tutorial and use an online tester to ensure you get the regex correct. Once you've got the basics, refer to this cheatsheet to try new features.

You may need to be much more specific in what you do and don't want to match. For example, asking to match http://www.facebook.com/johnsmith suggests that the user's name consists of letters, but are numbers and symbols valid?

Comments

0
var matchUserLink = function(name, id, s) {
  var baseUrl = 'http://www.facebook.com/';
  s = "" + s;
  return (s == (baseUrl + name.toLowerCase().replace(/\W/g, ''))) ||
    (s == (baseUrl + 'profile.php?id=' + id)) ||
    (s == (baseUrl + 'people/' + name.replace(/\W/g, '-') + '/' + id));
};

var js='John Smith', id=101, fb='http://www.facebook.com/';
matchUserLink(js, id, fb+'johnsmith'); // => true
matchUserLink(js, id, fb+'profile.php?id=101'); // => true
matchUserLink(js, id, fb+'people/John-Smith/101'); // => true

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.