email regex not working properly with javascript/angular [duplicate] - javascript

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 2 years ago.
I have created a regular expression for email validations.
var regex = "^([a-zA-Z]+([\.-_]?[a-zA-z0-9]+)*)\#([a-zA-Z0-9]+)([-][0-9a-z]+)?\.([a-z-]{2,20})(\.[a-z]{2,3})?$"
To match emails:
1. update#update
2. mohit.bhagat#B-9com.com
3. mohit.Bhagat#us.thalesgroup.com
4. mohit#gmail.com.com.com
If you run this over online website 1 and 4th will fail while 2, 3 will pass.
But when I run this code in Javascript( Browser console ), 1st also passes the validation.
I am using Angular application.

The problem is how JS ignores \.
If you do following
var regex = "[a-z]+#[a-z]+\.[a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+.[a-z]{2,3}"
And if you do this
var regex ="[a-z]+#[a-z]+[\.][a-z]{2,3}"
Resultant string stored is
"[a-z]+#[a-z]+[.][a-z]{2,3}"
Pay attention to [.] with this now i was able to get validation error for 1st email.
Complete regex: "^([a-zA-Z]+([-_.]?[a-zA-Z0-9])*)#([a-zA-Z0-9]+([-][a-z0-9A-Z]+)?)[.]([a-z]+[-_]?[a-z]+)([.][a-z]{2,3})?$"
Update:
Or you can do this: var regex ="[a-z]+#[a-z]+\\.[a-z]{2,3}"
As mentioned in comments below, you can do this way to consider . by including it in [.], but its a hack and not origional way to do it.
Best way to have . included in your regex is \.

Related

How can I cleanse a String to contain only digits (0-9) and the letter X (uppercase) only, using Java and Javascript [duplicate]

This question already has answers here:
Remove non-numeric characters except points, commas and '$'?
(2 answers)
Closed 2 years ago.
I am taking an ISBN input by the user - may contain spaces and hyphens etc. - and trying to sanitise it to be only digits.
In Java and Javascript, I have used the following regex successfully
Java (isbn is a java.lang.String)
isbn = isbn.replaceAll("[^\\d]", "");
and, JavaScript
isbn = isbn.replace(/[^\d]/g, "");
However, some ISBNs can have an X as their checksum character. For example, 'The book of days' by Sara Reinke is '155404295X'
How can I change the regex to allow X as well as digits?
Update: [^\dX] worked in JavaScript, but [^\\dX] does not work in Java.
Update 2: PEBKAC! I was sanitising in two places - I updated one but not the other. [^\\dX] does work in Java as well.
Can you try [^0-9X] there? I think it will work in both Java and JavaScript.
P.S. But \\d should work in Java too...
If you wanna follow your solution you can exclude with ?:(values)
For your example it would be: [^\d?:(X)] tested in online java regex.

Javascript Regex not working while used in a angular controller [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Match exact string
(3 answers)
Closed 5 years ago.
I have written the following function in javascript to validate a string that I will use as a file name. I would like to check for all the characters that are restricted by Windows OS as invalid while creating files. I checked the regular expression at RegExr, it seems to be working as expected but it doesn't work when called from an Angular controller and it only matches the first character in the parameter. I'm adding the file extension later on so that isn't a problem.
Can anybody help with it? I'm relatively new to regular expressions and would appreciate any help or pointers to useful resources.
function validateInput(value) {
if (!AngularUtils.isUndefinedOrNull(value)) {
var regex = new RegExp("[^<>/\\\\=|:*?\"]+$");
var regexOutput = regex.test(value);
if (!regex.test(value))
return true;
}
return false;
}
Edit:
Even after changing the regex to handle javascript constructors, I'm still getting valid matches for the following input: "sample_css", "sample=css","=sample"
Only the first string should be valid. jsfiddle here.

Regex capture everything between two tags across multiple lines [duplicate]

This question already has answers here:
JavaScript regex multiline text between two tags
(5 answers)
Closed 7 years ago.
I have this regex in Ruby: http://rubular.com/r/eu9LOQxfTj
/<sometag>(.*?)<\/sometag>/im
And it successfully matches input like this:
<sometag>
123
456
</sometag>
Which would return
123
456
However, when I try this in javascript (testing in chrome), it doesn't match anything.
Does javascript's multiline flag mean something else?
I want to capture everything non-greedily between two given tags.
How can I accomplish this in javascript using regex? Here is a Debuggex Demo
<sometag>(.*?)<\/sometag>
This is not XML parsing.
Javascript does not support multiline expressions when using . alone. You have to use [\s\S] in place of . so an example that satisfies what you want would be:
var x = "<sometag>\n\
123\n\
456\n\
</sometag>";
var ans = x.match(/<sometag>([\s\S]*?)<\/sometag>/im).pop();
// ans equals " 123 456"
note that you still need the m modifier.

Javascript regular expression for URL validation [duplicate]

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 8 years ago.
I want the regular expression to validate the URL with below condition,
It should start with http or https
It should end with the valid domain.
Ex: .com or .in but after the . can have any string. Specially, check whether the . is there and there is a string following the ..
Valid URL: http://www.cnn.com
Invalid URLS:
htt://www.yahoo.com
http://www.yahoo.
http://www.yahoo
I have compose the regular expression as below.
/^(http|https):\/\/[-a-zA-Z0-9+&##/%?=~_|!:,;]+([\-\.]{1}[-a-zA-Z0-9+&##/%?=~_|]+)*\.[a-zA-Z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
It worked fine for most of the scenarios.
But if I enter http://www.yahoo didn't validate correctly, but If I enter http://www.google it throws the validation error.
Can anybody please help me to resolve this issue?
Try this:
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
You can use the following reg exp to match the urls
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
Check it here
https://www.regex101.com/r/jU7iT2/1

how to parse values from comma separated values using regex for javascript [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 years ago.
I have
HOSTNAMEA,HOSTNAMEB,HOSTNAMEC,...
I have a third party workflow tool that can do the looping but can only use regex to parse values. I'd like to get a regex that grabs each hostname and puts into it's own variable in my workflow tool so the results will be
HOSTNAMEA
HOSTNAMEB
HOSTNAMEC
...
I'm struggling to get a regex that just grabs the text block X between the commas
ever heard of \w+ if you just want the strings between the comma, you can use .split(", ") as well
var str = "HOSTNAMEA,HOSTNAMEB,HOSTNAMEC";
var res = str.match(/\w+/g);
console.log(res.join(" "));
sample code for your help

Categories

Resources