I'm trying to match a letter (let's say a) that is not escaped with a backslash, but I want to do it without using negative lookaheads or negative lookbehinds, this is what I tried so far but it doesn't work
/([^\\][^a])*/.test('should be true a.'); // true
/([^\\][^a])*/.test('should be not true \\a.'); // true
But they both return true. What am I doing wrong?
To test for an 'a' which is not preceded by a '\' you could use
/(^|[^\\])a/.test( 'should be true a.' ); // true
/(^|[^\\])a/.test( 'should be not true \\a.' ); // false
The (^|[^\\]) matches either the start of the string ^ or a character that is not '\'.
In your regex, the [^a] matches any character that is not 'a', and ()* means match what is enclosed within the brackets zero or more times - so any string would test true, as any string could match the pattern zero times.
Related
I've been doing field validation that should allow a-z characters, spaces, hyphens and periods. The regex is:
/^[a-zA-Z-. ]+$/
For the most part, the following works; however, it fails if either - or . are repeated:
String = true,
Str- in.g = true,
String-- = false,
String... = false
I know that in some cases, the - and . should be escaped but I don't believe they need to be in this case as they are within the [ ].
It returns true for all the strings, what have you tried that's returning false for matching
let reg = /^[a-zA-Z-. ]+$/
let tests = ["String", "Str- in.g", "String--", "String...", "String...---str.ing---"]
tests.forEach((item) => {
console.log(`${item} : ${reg.test(item)}`)
})
From docs at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes:
A character class. Matches any one of the enclosed characters. You can specify a range of characters by using a hyphen, but if the hyphen appears as the first or last character enclosed in the square brackets, it is taken as a literal hyphen to be included in the character class as a normal character.
That is, if you use the hyphen not at the beginning or end, or as a range delimiter, it is in an undefined state, and depending on the regex implementation it will do one thing or another. So, unless in a range, always put the hyphen at the beginning or end, or \- escape it.
Fixed regex with tests:
[ 'String', 'Str- in.g', 'String--', 'String...', 'Not good!' ]
.forEach(str => {
let match = /^[a-zA-Z. -]+$/.test(str);
console.log(str, '==>', match);
});
Output:
String ==> true
Str- in.g ==> true
String-- ==> true
String... ==> true
Not good! ==> false
Hi guys I trying to check if password contains special caracters and at least 2 characters (digit or letter)
So like this:
("&#€$&&÷") ====> false
("&#&'&*#5") ====> false
(">~<<`<•5t") ====> true
("{\><>\tt") =====> true
("65%#^$*#") ====> true
("7373673") ====> false
("7267373~") ====> true
I've tried this regular expression but It seems not working:
/^((?=.*\d{2})|(?=.*?[a-zA-Z]{2}))/
You can assert the 2 occurrences of a character or digit in the same lookahead, and then match at least a single "special" character.
Using a case insensitive pattern:
^(?=(?:[^a-z\d\n]*[a-z\d]){2})[a-z\d]*[~!##$%^&*()_+<>•`{}\\][~!##$%^&*()_+<>•`{}\\a-z\d]*$
The pattern matches:
^ Start of string
(?:[^a-z\d\n]*[a-z\d]){2} Assert 2 occurrences of either a char a-z or a digit. The [^a-z\d\n]* part negates the character class using [^ to prevent unnecessary backtracking
[a-z\d]* Match optional chars a-z or a digit
[~!##$%^&*()_+<>•`{}\] Match a special character
[~!##$%^&()_+<>•`{}\a-z\d] Match optional allowed chars
$ End of string
Regex demo
I have the following code. I am trying to have the regular express test the phone number for validation. With the current argument the function should return positive, but it doesn't and I can't figure out why.
function telephoneCheck(str) {
let reg = /^[\d]{0,1}[\w]{0,1}[(]{0,1}[\d]{3}[-)\w]{0,2}[\d]{3}[-\w]{0,1}[\d]/;
return reg.test(str);
}
console.log("function: " + telephoneCheck("1 (555) 555-5555"));
Can anyone see what I am missing?
First, replace all the \w (Matches any letter, digit or underscore) with \s (Matches any space, tab or newline character). I believe you don't won't letter in phone number.
Second, you need to add a quantifier {0,4} to the end of your Regex, just like you already did in other positions of the Regex.
So the final Regex will be ^[\d]{0,1}[\s]{0,1}[(]{0,1}[\d]{3}[-)\s]{0,2}[\d]{3}[-\s]{0,1}[\d]{0,4}
Because your regex is nonsense.
No need for [] for single group ([\d]{0,1} can be just \d?
\w does not match spaces, just [a-z0-9] in general case
You match starting ( but ending ) can be followed by - or any \w
function telephoneCheck(str) {
let reg = /^\d?\s?\(?[\d-]+\)?\s?\d+/;
return reg.test(str);
}
console.log("function: " + telephoneCheck("1 (555) 555-5555"));
You need to replace \w with \s for whitespace
You need to escape parenthesis \( and \)
You need to change [-)\w]{0,2} to [-\)]{0,1}[\s]{0,1} unless you want the unorthodox 1 (555)-555-5555 to be true.
The final [\d] should be [\d]{4} since you want exactly 4 digits at the end.
You can replace the {0,1} with ? when evaluating a single character that is optional.
You don't need brackets around single characters.
Here's the resulting code:
function telephoneCheck(str) {
let reg = /^\d?\s?\(?\d{3}[-\)]{0,1}\s?\d{3}[-\s]?\d{4}/;
return reg.test(str);
}
console.log(telephoneCheck("1 (555)555-5555")); // true
console.log(telephoneCheck("1 (555)5555555")); // true
console.log(telephoneCheck("(555)555-5555")); //true
console.log(telephoneCheck("15555555555")); //true
console.log(telephoneCheck("1 (555)555- 5555")); // false
console.log(telephoneCheck("1 (555)-555-5555")); // false
I need to check whether a string contains a dot in between or not, and if there are more than 2 dots than there should be any chracter (including special characters) between them.
abc.def should return true
.abc.def should return false
abc..def should return false
abc.def.ghi should return true
abc should return false
abc. should return false
The regex you are searching for is:
^[^.]+(\.[^.]+)+$
That consists of:
^: begin of string
[^.]+: at least one not-period.
(...)+: at least one:
\.: literal period, followed by
[^.]+: at least one not-period
$: end of string
See demo
I need to validate a string that can have any number of characters, a comma, and then 2 characters. I'm having some issues. Here's what I have:
var str="ab,cdf";
var patt1=new RegExp("[A-z]{2,}[,][A-z]{2}");
if(patt1.test(str)) {
alert("true");
}
else {
alert("false");
}
I would expect this to return false, as I have the {2} limit on characters after the comma and this string has three characters. When I run the fiddle, though, it returns true. My (admittedly limited) understanding of RegExp indicates that {2,} is at least 2, and {2} is exactly two, so I'm not sure why three characters after the comma are still returning true.
I also need to be able to ignore a possible whitespace between the comma and the remaining two characters. (In other words, I want it to return true if they have 2+ characters before the comma and two after it - the two after it not including any whitespace that the user may have entered.)
So all of these should return true:
var str = "ab, cd";
var str = "abc, cd";
var str = "ab,cd";
var str = "abc,dc";
I've tried adding the \S indicator after the comma like this:
var patt1=new RegExp("[A-z]{2,}[,]\S[A-z]{2}");
But then the string returns false all the time, even when I have it set to ab, cd, which should return true.
What am I missing?
{2,} is at least 2, and {2} is exactly two, so I'm not sure why three characters after the comma are still returning true.
That's correct. What you forgot is to anchor your expression to string start and end - otherwise it returns true when it occurs somewhere in the string.
not including any whitespace: I've tried adding the \S indicator after the comma
That's the exact opposite. \s matches whitespace characters, \S matches all non-whitespace characters. Also, you probably want some optional repetition of the whitespace, instead of requiring exact one.
[A-z]
Notice that this character range also includes the characters between Z and a, namely []^_`. You will probably want [A-Za-z] instead, or use [a-z] and make your regex case-insensitive.
Combined, this is what your regex should look like (using a regular expression literal instead of the RegExp constructor with a string literal):
var patt1 = /^[a-z]{2,},\s*[a-z]{2}$/i;
You are missing ^,$.Also the range should be [a-zA-Z] not [A-z]
Your regex should be
^[a-zA-Z]{2,}[,]\s*[A-Za-z]{2}$
^ would match string from the beginning...
$ would match string till end.
Without $,^ it would match anywhere in between the string
\s* would match 0 to many space..