Social
Facebook page URL Regex
Facebook pages and profiles with a custom username are reached at facebook.com followed by that name. Usernames contain letters, digits and dots and must be at least five characters. This pattern accepts that form, with optional https, www or m subdomains, the fb.com short domain and an optional trailing slash. It fits business directories, contact forms and social link fields where you want a clean page link. It rejects profile.php links with numeric IDs and deep links to posts or photos. Facebook also reserves some words, so a well-formed link may still not belong to any page.
Pattern (JavaScript)
/^(?:https?://)?(?:www\.|m\.)?(?:facebook|fb)\.com/[A-Za-z0-9.]{5,50}/?$/Starts at the start of the line/string, then (the text "http", then the character "s", optionally (zero or one time), then the text "://"), optionally (zero or one time), then (the text "www." OR the text "m."), optionally (zero or one time), then (the text "facebook" OR the text "fb"), then the text ".com/", then one of the characters A-Za-z0-9., between 5 and 50 times, then the character "/", optionally (zero or one time), then the end of the line/string.
Examples
Matches
- ✓ https://www.facebook.com/TestPage
- ✓ facebook.com/sample.page.01/
- ✓ https://m.facebook.com/examplebakery
- ✓ fb.com/TestPage
Does not match
- ✗ https://www.facebook.com/abc
- ✗ https://www.facebook.com/test-page
- ✗ https://example.com/TestPage
- ✗ https://www.facebook.com/profile.php?id=1
Try it
0 matches (JavaScript engine)
https://www.facebook.com/TestPage
facebook.com/sample.page.01/
https://m.facebook.com/examplebakery
fb.com/TestPage
https://www.facebook.com/abc
https://www.facebook.com/test-page
https://example.com/TestPage
https://www.facebook.com/profile.php?id=1Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:https?:\/\/)?(?:www\.|m\.)?(?:facebook|fb)\.com\/[A-Za-z0-9.]{5,50}\/?$/g;
const text = "https://www.facebook.com/TestPage";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:https?://)?(?:www\.|m\.)?(?:facebook|fb)\.com/[A-Za-z0-9.]{5,50}/?$')
text = 'https://www.facebook.com/TestPage'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#^(?:https?://)?(?:www\.|m\.)?(?:facebook|fb)\.com/[A-Za-z0-9.]{5,50}/?$#';
$text = 'https://www.facebook.com/TestPage';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:https?://)?(?:www\\.|m\\.)?(?:facebook|fb)\\.com/[A-Za-z0-9.]{5,50}/?$");
String text = "https://www.facebook.com/TestPage";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:https?://)?(?:www\.|m\.)?(?:facebook|fb)\.com/[A-Za-z0-9.]{5,50}/?$`)
text := "https://www.facebook.com/TestPage"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:https?://)?(?:www\.|m\.)?(?:facebook|fb)\.com/[A-Za-z0-9.]{5,50}/?$");
var text = "https://www.facebook.com/TestPage";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:https?://)?(?:www\.|m\.)?(?:facebook|fb)\.com/[A-Za-z0-9.]{5,50}/?$").unwrap();
let text = "https://www.facebook.com/TestPage";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:https?:\/\/)?(?:www\.|m\.)?(?:facebook|fb)\.com\/[A-Za-z0-9.]{5,50}\/?$/
text = 'https://www.facebook.com/TestPage'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are hyphens rejected in Facebook usernames?
- Facebook usernames allow letters, digits and dots only. A link with a hyphen in that position is usually a typo or a different kind of URL, so the pattern rejects it.
- How do I accept profile.php?id= links too?
- Add an alternative such as profile\.php\?id=\d+ after facebook.com/. Those links use a numeric ID because the page or profile has no custom username.
- Why is the minimum length five characters?
- Facebook requires custom usernames to be at least five characters long. Shorter paths after facebook.com are usually reserved site sections rather than pages.
Related patterns
- Discord invite link^(?:https?://)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com/invite)/[A-Za-z0-9-]{2,32}/?$
- GitHub repository URL^(?:https?://)?(?:www\.)?github\.com/[A-Za-z0-9](?:-?[A-Za-z0-9]){0,38}/[A-Za-z0-9._-]+?(?:\.git)?/?$
- LinkedIn profile URL^(?:https?://)?(?:[a-z]{2,3}\.)?linkedin\.com/in/[A-Za-z0-9-]{3,100}/?$
- YouTube channel URL^(?:https?://)?(?:www\.)?youtube\.com/(?:@[A-Za-z0-9._-]{3,30}|channel/UC[A-Za-z0-9_-]{22}|c/[A-Za-z0-9_-]+)/?$