Javascript form validation/sanitizing do i need regex here? - javascript

I have a single form input that is for checking domains. Sometimes people type in www. before the domain or .com after the domain name. The service that i use to check availability automatically checks for all top level domains so when people add the .com at the end it becomes redundant. For example the string submitted is domainname.com.com which is clearly invalid.
I understand you can do this on the server side but due to some rather weird circumstance i must use javascript for this. So is regex the solution here ? If so is there some kind of regex generator i can use for this or can someone point me in the right direction with a code snippet perhaps ?
Appreciate any help thanks!

This page has an example Regex.
function isUrl(s) {
var regexp = /^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/
return regexp.test(s);
}
Here is another example.
function isUrl(s) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/
return regexp.test(s);
}

Well, regex is one possible solution. You can peel off common TLD's like this:
input = input.replace(
/\.(?:com|net|org|biz|edu|in(?:t|fo)|gov|mil|mobi|museum|[a-z][a-z])$/i, "");
Is that the kind of thing you're looking for?

Related

Regex expression to match certain url behavior in my website

I have the following url
https://myurl/blogs/<blog-category>/<blog-article>
I've trying to create a regEx so i can thrigger a script only when i'm in an article.
i tried this among other tests but it didn't work and i'm not really the best guy building RegExs.
window.location.pathname.match(/\/blogs\/^[a-zA-Z0-9_.-]*$\/^[a-zA-Z0-9_.-]*$/
So in my understanding the first part of this regEx (\/blogs\/) is trying just to match a fixed string.
Then next parts just tries to match any kind of numeric,character and _.- combination (which is basically the potential strings that i can have there)
However this is not working at all.
My piece of script is looking like this
if(window.location.pathname.match(/\/blogs\/^[a-zA-Z0-9_.-]*$\/^[a-zA-Z0-9_.-]*$/){
// A code implementation here
}
Note: One thing that i noticed when writing this is that if i remove everything and just try
window.location.pathname.match(/\/blogs\/)
It doesn't work either.
Can someone help me solve this? I will also appreciate any guide that can help me improve my RegEx skills.
Thanks!
Update: to have this working i had to separate my condition into two things to get it to work properly.
It ended up looking like this:
var path = window.location.pathname;
const regEx = /\/blogs\/[a-zA-Z0-9_.-]*\/[a-zA-Z0-9_.-]*/i;
if(path.match(regEx)){
// My code here
}
This should work:
\/blogs\/[a-zA-Z0-9_.-]*\/[a-zA-Z0-9_.-]*
the "^" symbol checks that it is the start of a string which is not the case for the url in question
I would suggest using https://regexr.com/ for testing your regex to remove any other possible issues from other code
var patt = /\/blogs\/[a-zA-Z0-9_.-]*\/[a-zA-Z0-9_.-]*/i window.location.pathname.match(patt)
You can try using this

Regex to exclude specific websites in javascript?

I have a regex which matches all the websites but i want to exclude 2 specific websites from this regex?
Regex is
[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)
Websites I want to exclude are
www.gfycat.com
www.imgur.com
imgur.com/*
gfycat.com/*
Is it possible to write the regex which exludes the specific websites? Any suggestions on how to solve this problem?
/[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)/
I have attached the screenshot for match patterns.
Using RES and regex need to be implemented here.
Try this
^(?:(?!(?:www\.)?(?:google|gfycat|imgur))[-a-zA-Z0-9#:%._\+~#=]{2,256})\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)
Not sure, why you need regex to do the same. Can you not do something simple like the below, unless I understood it completely wrong.
url = new URL('https://www.google.co.uk/?gfe_rd=cr&ei=bN5IWaP7CYyDtAHKv4CIBg#q=hello');
if url.hostname == 'www.google.com'
// ignore
else
// process
The answer is not relevant to the specific question as OP is using a different tool

javascript Reg Exp to match specific domain name

I have been trying to make a Reg Exp to match the URL with specific domain name.
So if i want to check if this url is from example.com
what reg exp should be the best?
This reg exp should match following type of URLs:
http://api.example.com/...
http://preview.example.com/...
http://www.example.com/...
http://purhcase.example.com/...
Just simple rule, like http://{something}.example.com/{something} then should pass.
Thank you.
I think this is what you're looking for: (https?:\/\/(.+?\.)?example\.com(\/[A-Za-z0-9\-\._~:\/\?#\[\]#!$&'\(\)\*\+,;\=]*)?).
It breaks down as follows:
https?:\/\/ to match http:// or https:// (you didn't mention https, but it seemed like a good idea).
(.+?\.)? to match anything before the first dot (I made it optional so that, for example, http://example.com/ would be found
example\.com (example.com, of course);
(\/[A-Za-z0-9\-\._~:\/\?#\[\]#!$&'\(\)\*\+,;\=]*)?): a slash followed by every acceptable character in a URL; I made this optional so that http://example.com (without the final slash) would be found.
Example: https://regex101.com/r/kT8lP2/1
Use indexOf javascript API. :)
var url = 'http://api.example.com/api/url';
var testUrl = 'example.com';
if(url.indexOf(testUrl) !== -1) {
console.log('URL passed the test');
} else{
console.log('URL failed the test');
}
EDIT:
Why use indexOf instead of Regular Expression.
You see, what you have here for matching is a simple string (example.com) not a pattern. If you have a fixed string, then no need to introduce semantic complexity by checking for patterns.
Regular expressions are best suited for deciding if patterns are matched.
For example, if your requirement was something like the domain name should start with ex end with le and between start and end, it should contain alphanumeric characters out of which 4 characters must be upper case. This is the usecase where regular expression would prove beneficial.
You have simple problem so it's unnecessary to employ army of 1000 angels to convince someone who loves you. ;)
Use this:
/^[a-zA-Z0-9_.+-]+#(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?
(domain|domain2)\.com$/g
To match the specific domain of your choice.
If you want to match only one domain then remove |domain2 from (domain|domain2) portion.
It will help you. https://www.regextester.com/94044
Not sure if this would work for your case, but it would probably be better to rely on the built in URL parser vs. using a regex.
var url = document.createElement('a');
url.href = "http://www.example.com/thing";
You can then call those values using the given to you by the API
url.protocol // (http:)
url.host // (www.example.com)
url.pathname // (/thing)
If that doesn't help you, something like this could work, but is likely too brittle:
var url = "http://www.example.com/thing";
var matches = url.match(/:\/\/(.[^\/]+)(.*)/);
// matches would return something like
// ["://example.com/thing", "example.com", "/thing"]
These posts could also help:
https://stackoverflow.com/a/3213643/4954530
https://stackoverflow.com/a/6168370
Good luck out there!
There are cases where the domain you're looking for could actually be found in the query section but not in the domain section: https://www.google.com/q=www.example.com
This answer would treat that case better.
See this example on regex101.
As you you pointed you only need example.com (write domain then escaped period then com), so use it in regex.
Example
UPDATED
See the answer below

Make regex betterer

I need an expression that covers two eventualities:
www.example.com
knowledge.example.com
There are many other possible subdomains so it needs to be specifically either the root domain or the knowledge domain.
I did have a go and this appears to work. But it looks long and unsightly and I wondered if there was a more elegant regex:
(www\.)?(knowledge\.)?(example\.com)
It's not that long and ugly, I suppose. I'm just curious if I'm approaching it right or if there's a shorter way of writing it.
This is slightly less ugly, in my opinion:
(www|knowledge)\.(example\.com)
Sometimes I prefer this:
(www|knowledge)[.](example[.]com)
Full equivalent of yours regexp:
((?:www|knowledge)\.)?(example\.com)
Bonus answer,
You can use conditionals with in your regex pattern,
Eg. (?(?!www\.)knowledge|www)(?:\.example\.com)
Working demo # regex101
Edit I
For regex engines that do not support conditionals, below is the workaround used to mimic the if-else flow:
((?(?=positive-regex-statement)then|(?!negavite-regex-statement)then)
((?=www\.)www|(?!www\.)knowledge)(?:\.example\.com)
Working demo # regex101-javascript-conditionals
Regex is not the most readable thing in the world. If you want something more clear in its meaning, try this:
var domain = 'www.example.com';
var subdomain = domain.replace(/(\w+)\.example\.com/, '$1');
var validSubdomains = ['www', 'knowledge'];
validSubdomains.indexOf(subdomain) != -1;

JavaScript Regex E-Mail Validation

I'm really new to regex in general. All I need is it to check and make sure (Something#something.something) works. I've tried this.
var checkEmail = /^\w+#\w+.[a-zA-Z]/;
Is something like this correct for what I'm looking for?
To refine what you have:
var checkEmail = /^\w+#\w+\.[a-zA-Z]+/;
What you posted it close (you should escape the . so it doesn't match any character and add a + after the [a-zA-Z] because top-level domains are at least 2 character I think), but for something like an email address that actually has a long and little known spec, I would just use someone else's regex.
Here's a site with more info:
http://www.regular-expressions.info/email.html
You should escape the dot, otherwise it is a meta character that matches anything. Try:
/^\w+#\w+.[a-zA-Z]/

Categories

Resources