0

Some users using URLs (Domains) in there usernames. They are sometimes much different, so i want to filter them out in there name

I can str.replace urls, if i know the urls, but there much different so i think regex is, how it works, but i try so much, but doesnt get any results...

I hope someone can help me.

I found that Regex for PHP but i need something for NodeJS / Javascript.

 $regex = '/^(.*?)';
 $regex .= "((https?|ftp)\:\/\/)?"; // SCHEME 
 $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass 
 $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP 
 $regex .= "(\:[0-9]{2,5})?"; // Port 
 $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path 
 $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query 
 $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor 
 $regex .= '(.*?)$/i';

A username looks like

USERNAME Google.com

but can also

USERNAME www.GOOGLE.com

or

USERNAME GOOGLE.GG
4
  • 1
    You could block . in username. Even with regex users could do something like google(.)com, google . com etc. Commented Dec 5, 2018 at 13:21
  • What you mean? If i replace "." in the name, than it looks Like USERNAME GOOGLEcom Not really better :/ Commented Dec 5, 2018 at 13:22
  • Just don't let them use . in their username when registering. It's just a suggestion, not the only solution. You could create a massive regex to check different combinations. Commented Dec 5, 2018 at 13:23
  • I use an API. I cannot check or let them create usernames. I get this from Login API Commented Dec 5, 2018 at 13:35

1 Answer 1

1

You can use this regex to check for URL. It's basic. You can extend it if you need.

((http|ftp|https):\/\/)?([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?

https://regex101.com/r/fMAMkA/1

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

2 Comments

Hm thanks. But how to use it? Can you create a JSFiddle with my example USERNAME Google.com? I tried but not working jsfiddle.net/u3bo9kdf/1
Remove ^ from regex to not look from start of a line. jsfiddle.net/0y2ftk8a

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.