how to write a regular expression that ONLY accepts strings [duplicate] - javascript

This question already has answers here:
Regular Expression to match only alphabetic characters
(5 answers)
Closed 7 years ago.
I am trying to understand regular expressions, i find it really confusing to understand it. I need to know how to write a regular expression that ONLY matches strings, NO NUMBERS, NO SPECIAL CHARACTERS, just letters from A - Z. I have to say that I tried using \[a-z]\ but it didn't work as i wanted. These are some of the results that I want:
pe<.?ter -----> should return false
ale8 ---------> should return false
?ramon -------> should return false
tho<>?mass----> should return false
peter -----> should return true
alex ------> should return true
ramon -----> should return true
thomas ----> should return true

This should work for you:
var re = /^[a-z]+$/i;
See here

Your regex returns true if it find ONE letter from a to z, only lowercase, in your string.
To make what you want, use /^[a-zA-Z]+$/ or /^[a-z]+$/i.
^ make that your regex start from the begining of the string.
[a-zA-Z] make the regex to find one letter, lowercase OR uppercase (note that you could use instead the i modifier which make the search case insensitive).
+ make the regex to search for one or more times the previous element.
$ make sure that your regex end at the end of the string.

Related

numbers cannot follow characters

I'm trying to match a string that numbers cannot follow characters
For example, these should all match:
abc-123
123-abc
123-abc-123
abc-123-abc
not match:
abc123
123abc
abc1
a1b2
Please help me to find a javascript regex.
To match strings that contain only letters separated by numbers, you can use the following regular expression:
/^[a-zA-Z]+(?:-[a-zA-Z]+)*$/
This regular expression will match strings that start and end with letters, and have a number in between. The letters and the number must be separated by a hyphen.
For example, you can use the following code to test this regular expression:
const regex = /^[a-zA-Z]+(?:-[a-zA-Z]+)*$/;
console.log(regex.test("abc-123")); // should return true
console.log(regex.test("123-abc")); // should return true
console.log(regex.test("123-abc-123")); // should return true
console.log(regex.test("abc-123-abc")); // should return true
console.log(regex.test("abc123")); // should return false
console.log(regex.test("123abc")); // should return false
console.log(regex.test("abc1")); // should return false
console.log(regex.test("a1b2")); // should return false
This code defines a regular expression using the pattern described above, and then uses the test method to check if various strings match the pattern. The test method returns a boolean indicating whether the string matches the pattern or not.
Edited, since the question changed (I am still not sure if it is correct).

Return true if expression B follows A [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 2 years ago.
I am trying to find an solution to return a true value if certain expression B which follows expression A is valid.
For instance -
If I'm trying to match the strings with the regex - F[A-Z]{0,2}
F
FA
FB
FAA
FAAA
where F is expression A here and [A-Z]{0,2} is expression B here
It is matching FAAA which it shouldn't since I have mentioned an quantifier max limit to 2.
So the expected output is -
F
FA
FB
FAA
JSFiddle
You need to either use:
\bF[A-Z]{0,2}\b
or
^F[A-Z]{0,2}$
you get true currently because a match does occur. You need some sort of limitation on the matching.
F[A-Z]{0,2} says match an F then 0 to 2 uppercase alpha characters. Anything before or after that can still exist.
See https://regex101.com/r/KLKTS4/2/ for a demo.
Adding a $ to the end of your regex should fix it:
F[A-Z]{0,2}$
https://regex101.com/r/1ADic0/2
The $ sign will make sure that you are at the end of line. You can see a lot of regexps starting with ^ and ending with $ (or \A and \z for multiline). This pattern basically means: my regexp should match the whole string.
You need to use $ in the end to asserts position at the end of a line and also need to put ^ to asserts position at the start of a line, so that values like BFA, BF could also be ignored. So the update regex would be like:
^F[A-Z]{0,2}$
DEMO

Regular expression to exclude alphabetic characters

Can anyone suggest a correct regex pattern in angular 2 for accepting numbers and special charterers for angular 2 input field , excepting alphabets
This question has nothing to do with angular, but you can accomplish this with plain javascript. The regexp object's test method returns a boolean showing whether or not your string matches the regular expression.
/^[^a-z]+$/i.test("!##$%") == true // true
/^[^a-z]+$/i.test("Hello") == true // false
In this regex, the initial ^ is the beginning of line anchor and the trailing $ is end of line. The rest of the expression matches 1 or more characters that's not alphabetical. The i flag at the end makes it case-insensitive.
There are a number of nice online regex testing tools. One is https://regex101.com/.

Regular expression seems to treat one string as multiple substrings [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
Why the following code returns "ZZZCamelCase"?? Doesn't such regex examine if the string starts and ends with small case a-z? As what I understand, the str variable should match such condition, so the console output should be "ZZZZZZZZZZZZ", but obviously it somehow breaks the str, and examine the substring against the regex. Why? and how can I tell the program to treat "testCamelCase" as one string?
var str = "testCamelCase";
console.log(str.replace(/^[a-z]+/, 'Z')); // ZZZCamelCase
Here you are matching one or more lower case letters. That's going to be 'test' in your string, because after that comes an uppercase 'C'. So only 'test' gets rpelaced by 'ZZZ'
console.log(str.replace(/^[a-z]+/, 'ZZZ')); // ZZZCamelCase
Use
str.replace(/[a-z]/ig, 'Z')
to get 'ZZZZZZZZZZZZ'
You are forgetting, that regex is case sensitive. This means, that [a-z] doesn't capture the whole string. [a-zA-Z] does. So does [\w] including digits from 0-9.

Perfect solution to validate a password [duplicate]

This question already has answers here:
regular expression for password with few rules
(3 answers)
Closed 8 years ago.
I took an online JavaScript test where the 3rd problem was as follows:
Complete the checkPassword function, which should check if the password parameter adheres to the following rules:
Must be longer than 6 characters.
Allowed characters are lower or uppercase Latin alphabetic characters (a-z), numbers (0-9), and special characters +, $, #, \, / only.
Must not have 3 or more consecutive numbers (e.g. "pass12p" is fine,
but "pass125p" is not, because it contains "125")
>
My solution was as follows:
function checkPassword(password) {
return /^[a-zA-Z0-9\+\$\\\/#]{6,}$/.test(password) && !/[0-9]{3,}/.test(password);
}
This solution gave me the correct outputs for the given inputs.
But the ultimate test result told that the solution is just 75% correct sadly.
I think the perfect answer is expected to be a solution just with a single regular expression. Is there anyone who can give me a single regular expression that gives the same result? I am not so good at regular expression so please advise.
I appreciate your help in advance.
Just use a negative lookahead at the start.
^(?!.*?\d{3})[a-zA-Z0-9\+\$\\\/#]{6,}$
(?!.*?\d{3}) at the start asserts that the match won't contain atleast three consecutive digits. Then the regex engine tries to match the pattern [a-zA-Z0-9\+\$\\\/#] against the input string 6 or more times only if this condition is satisfied.
DEMO
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6,}$
For consecutive number check
public boolean isValid(final String userName,final String password) {
for(int i=0;(i+2)<userName.length();i++) if(password.indexof(userName.substring(i,i+2))!=-1) return false; return true;
}

Categories