Validate twitter URL - javascript

I am trying to validate a twitter url, so that at least it contains a username. I do not care if it exists or not, just that there is one.
I am using the below javascript regex
var re = new RegExp('((http://)|(www\.))twitter\.com/(\w+)');
alert(re.test('http://twitter.com/test_user'));
but it is not working.
The strange thing is that I created and tested the above regex at this URL
http://www.regular-expressions.info/javascriptexample.html
where it works just fine.
Any ideas?
Thanks

function isTwitterHandle(handle) {
if (handle.match(/^((?:http:\/\/)?|(?:https:\/\/)?)?(?:www\.)?twitter\.com\/(\w+)$/i) || handle.match(/^#?(\w+)$/)) return true;
return false;
}

You need to escape the backslashes in the escape sequences too:
var re = new RegExp('((http://)|(www\\.))twitter\\.com/(\\w+)');
And I would recommend this regular expression:
new RegExp('^(?:http://)?(?:www\\.)?twitter\\.com/(\\w+)$', 'i')

It's because of the way you're defining the regex by using a string literal. You need to escape the escape characters (double backslash):
'^(http://)?(www\.)?twitter\.com/(\\w+)'
In the above, I also changed the start so that it would match http://www.twitter.com/test_user.
Alternatively, use the RegExp literal syntax, though this means you have to escape /:
var re = /^http:\/\/)?(www\.)?twitter\.com\/(\w+)/;

-http://twitter.com/username (this is http)
-https://twitter.com/username (this is https)
-twitter.com/username (without http)
-#username( with #)
-username (without #)
var username = "#test";
var r1 = new RegExp('^((?:http://)?|(?:https://)?)?(?:www\\.)?twitter\\.com/(\\w+)$', 'i');
if (r1.test(username) == false) {
var r2 = new RegExp('^#?(\\w+)$', 'j');
if (r2.test(username) == true)
return true;
else
return false;
} else {
return true;
}

Related

How to exclusively detect subdomains of a URL with a regular expression

I am making a chrome extension that is given a list of domains that needs to be compared against the active URL of a tab. For example if the list of domains has
"google" then the extension should detect "docs.google.com" as part of the domain list. I have gotten this part to work. The issue is when the domain list contains a subdomain. For example: if "docs.google" is on the list then if the user is on "google.com" the extension should not recognize this as a URL on the domain list.
I am attempting this by constructing a regular expression. for each domain and subdomain. As I said, when you are given a domain (as opposed to a subdomain) it works properly although I have tested this with subdomains and it does not seem to work. I assume the issue is with how I constructed the RegEx. Anything that stands out? thank you in advance!
let onDomainList = false;
for(let i = 0; i < domainListLength-1; i++){
if(!domainList[i].includes(".")){ //if this domain is not a subdomain
let strPattern = "^https://www\\." + list.domainList[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "|https://[a-z_]+\\." + list.domainList[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
let domainRegEx = new RegExp(strPattern,'i');
if(domainRegEx.test(activeTab.url)){
onDomainList = true;
execute_script(activeTab);
}
} else{ //if this domain is a subdomain
let strPattern = "^https://www\\." + list.domainList[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
let domainRegEx = new RegExp(strPattern,'i');
if(domainRegEx.test(activeTab.url)){
onDomainList = true;
execute_script(activeTab);
}
}
}
EDIT: Changed RegEx to what Wiktor Stribizew suggested, although still the issue of not detecting subdomains.
Here is a fixed snippet:
let onDomainList = false;
for (let i = 0; i < domainListLength - 1; i++) {
if (!domainList[i].includes(".")) { //if this domain is not a subdomain
let strPattern =
let strPattern = "^https://www\\." + domainList[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "|https://[a-z_]+\\." + domainList[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
let domainRegEx = new RegExp(strPattern, 'i');
if (domainRegEx.test(activeTab.url)) {
onDomainList = true;
execute_script(activeTab);
}
} else { //if this domain is a subdomain
let strPattern = "^https://(?:[^\\s/]*\\.)?" + list.domainList[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
let domainRegEx = new RegExp(strPattern, 'i');
if (domainRegEx.test(activeTab.url)) {
onDomainList = true;
execute_script(activeTab);
}
}
}
Notes:
Since you are using a RegExp constructor notation, and define the regex with a regular string literal, you need to properly introduce backslashes used to escape special chars. Here, there is no need to escape / and the . needs two backslashes, the "\\." string literal is actually a \. text
The variable texts need escaping to be used properly in the code, hence domainList[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
The / before ^ renders the regex useless since there can be no / before the start of string, and thus /^ is a regex that never matches any string. / as regex delimiters should not be used in RegExp constructor notation
A subdomain regex does not actually match anything but https://www. + the domain from your list. To allow anything before the domain, you can replace www\. with (?:[^\s/]*\.)? that matches an optional sequence ((?:...)? is an optional non-capturing group) of zero or more chars other than whitespace and / (with the [^\/s]* negated character class) and then a dot.

Javascript Regular Expression with multiple conditions

I am using Nginx njs module for some url modifications.
My use case is to return the redirection uri for the given uri.
URI's will be as follows:
/books
/books/economic-genious
/books/flight-mechanics
My regular expression to match the above URI's as follows -
/books/(.*)|/books$
First part of expression /books/(.*) is to match below URI's:
/books/economic-genious
/books/flight-mechanics
Second part of expression /books$ is to match below URI's:
/books
My destination is configured as follows: /ebooks/$1. So that the above URI's will be converted to:
/ebooks
/ebooks/economic-genious
/ebooks/flight-mechanics
Javascript code:
function getMappedURI(uri) {
var exp = new RegExp('/books/(.*)|/books$');
var destUri = '/ebooks/$1';
var redirectUri = uri.replace(exp, destUri);
return redirectUri;
}
Above code is working fine for the below URI's:
/books/economic-genious
/books/flight-mechanics
But for the URI /books, it should return /ebooks/. But it is appending some non printable special character at the end of /ebooks/.
I think it is trying to replace $1 with some special character.
How to avoid adding of special character at the end ?
Try with this regex: \/books(\/(.*))?$
Demo here...
code:
function getMappedURI(uri) {
var exp = new RegExp('\/books(\/(.*))?$');
var destUri = '/ebooks$1';
var redirectUri = uri.replace(exp, destUri);
return redirectUri;
}
The OR | operator only works in parens. So you should make the match to (/books/(.*)|/books$) and I don't think the $ word match because, for anything to be matched It should be in parens, too, making the new match URL: (/books/(.*)|/books). You'll then have to use $2 instead of $1 as substitute instead.
function getMappedURI(uri) {
var exp = new RegExp('(/books/(.*)|/books)');
var destUri = '/ebooks/$2';
var redirectUri = uri.replace(exp, destUri);
return redirectUri;
}
But, if you want to want everything from /books/foo to /ebooks/foo, use this instead: /books/(.*) with $1 as substitute.
function getMappedURI(uri) {
var exp = new RegExp('/books/(.*)');
var destUri = '/ebooks/$1';
var redirectUri = uri.replace(exp, destUri);
return redirectUri;
}

JavaScript split string around emails

So I have this list of emails:
john.doe#doe.com
john.doe#doe.com
john.doe#doe.com
john.doe#doe.com
john.doe#doe.com
john.doe#doe.com
john.doe#doe.com
john.doe#doe.com
It comes through as a string, sometimes with a return character at the end of each line, sometimes it doesn't. All I want to be able to do is pull out each email from the string using regex.
So I've got this regex exp from here:
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
How do I get it to match many email addresses and return me each individual email?
I do not want to split on a return character, I can't always garauntee that it will be that character that will split the list up. These emails are pasted in from the users clipboard. If it were that easy, I wouldn't have asked ;)
It comes through as a string, with a return character at the end of
each line.
Then just split the string on newlines ?
var email_array = str.split("\n");
Here's a VERY simple way to do it.
/([^;:<>!?\n]+\#[^;:<>!?\n]+\.[^;:<>!?\n]+)/gmi
Explanation:
The [^;:<>!?\n] matches everything EXCEPT those characters. So [^;:<>!?\n]+ just means match everything but these as many times as needed.
Then match an # symbol.
Then match as many of NOT these ([^;:<>!?\n]) as needed again.
Then match a literal dot (.).
Then DON'T match these ([^;:<>!?\n]) again.
The gmis at the end are called flags. They mean:
g means global. Match this RegEx over and over.
m means multi-line. Don't stop at the end of the first line of emails.
i means insensitive. Don't worry about the upper and lower cases.
Demonstrations here: https://regex101.com/r/aC5cK2/1
So I have re work the answer to incorporate what #adeneo said:
$scope.pasteThis = function(e) {
var emails = e.clipboardData.getData('text/plain');
var emailArray = emails.split(/(\n|\s|,)/);
angular.forEach(emailArray, function (e) {
var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+#[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
if (EMAIL_REGEXP.test(e)) {
if (!contains($scope.emailAddresses, e)) {
$scope.emailAddresses.push(e);
}
}
});
}
function contains(arr, el) {
var found = false;
angular.forEach(arr, function(e) {
if (e == el) {
found = true;
}
});
return found;
}
So EMAIL_REGEXP is from the Angular source code. I use that in other places so it is very appropriate to use it here (consistency).
This function makes sure that after the emails are split, each one is a valid email address. This means that no mess can get through.

why is my regular expression not validating a valid expression?

I am at a lost as to why this will not.
here is my regular expression:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|`¬':;~{}<>()#?!#$%^&*-]).{8,20}$
here is some code to simply test it:
var str1 = "AAbb123.";
var str2 = "ell";
var re = new RegExp("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|\`¬':\;\~{}<>()#?!\#$\%^&*-]).{8,20}$");
if(str1.match(re)){
alert("matched")
}
else {
alert("doesnt match")
}
the regular expression has been validated in 2 regular expression web sites (regexpal.com & http://www.freeformatter.com/regex-tester.html). both say str1 is valid for this expression but yet when included in my code it will not work.
below is another place I am trying to get the code working. and it keeps printing: requirements not met.
var uname = document.getElementById("pword1").value;
var re = new RegExp ("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|\`¬':\;\~{}<>()#?!\#$\%^&*-]).{8,20}$");
if(uname.match(re)){
DIMR = "Requirements MET";
}else {
DIMR = "Requirements NOT MET";
}
You need to properly escape a string when using new RegExp constructor.
Since you don't have any variables inside your pattern try
var str1 = "AAbb123.";
var str2 = "ell";
var re = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\+\=\.\[\]_£|\`¬':\;\~{}<>()#?!\#$\%^&*-]).{8,20}$/;
if(str1.match(re)){
alert("matched")
}
else {
alert("doesnt match")
}
Escaping only few characters present inside the character class would be enough. When using " as regex delimiter, you need to escape the backslash in your regex one more time.
var re = new RegExp("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[\\\\+=.\\[\\]_£|`¬':;~{}<>()#?!#$%^&*-]).{8,20}$");
special characters like +, ., * inside a character class would must match a literal + or . or *, so you don't need to escape it. To match a literal \, you need to escape that \ exactly three times.

Trouble getting my regular expression to match

I am having some trouble with my regex in javascript.
I have the following code, that I think should match, but it doesn't.
var rgx = new RegExp("{\d+:(\d+)}");
if (rgx.test("{0:00000}") == true) {
alert("match");
}
else
{
alert("no match");
}
​I am unsure if I should use test() here. I really want to catch the group, in my regex but exec() seems to give me the same result.
So what am I doing wrong?
The problem is that you need to escape the \ character in your regex:
var rgx = new RegExp("{\\d+:(\\d+)}");
Alternatively, you can use the literal syntax:
var rgx = /{\d+:(\d+)}/;
To capture the results, you should also use the .match function as opposed to test or exec. It will return null if it doesn't match and an array of at least one element if it does match.
There are multiple issues with the regex:
var rgx = new RegExp("{\d+:(\d+)}");
First (first noted by syazdani), you must string-escape the backslashes:
var rgx = new RegExp("{\\d+:(\\d+)}");
or better yet use a regex literal:
var rgx = /{\d+:(\d+)}/
Second, { and } have a special meaning in regex and should be escaped:
var rgx = /\{\d+:(\d+)\}/
Third, as noted by Ian, you might want to ensure the entire string is matched:
var rgx = /^\{\d+:(\d+)\}$/
RegExp#test returns a boolean true/false whether the string matches.
RegExp#exec returns an array holding the match and all captured groups if the string is matched, or null if the string is not matched:
var matches = /\{\d+:(\d+)\}/.exec("{0:000000}");
if(matches){
console.log(matches[1]); //logs "000000"
}

Categories