pattern password javascript - javascript

I'm working on a pattern for a password with the following requirements:
Min character = 6
Max character = 64
Min 1 lowercase character
Min 1 uppercase character
Min 1 number
Min 1 special characters
I am using this regex:
var passReg = /^(?=^[ -~]{6,64}$)(?=.*([a-z][A-Z]))(?=.*[0-9])(.*[ -/|:-#|\[-`|{-~]).+$/;
However, it does not work as expected.

You must be looking for this regex:
^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[ -/:-#\[-`{-~]).{6,64}$
See demo
Here is explanation:
^ - Beginning of string
(?=.*[a-z]) - A positive look-ahead to require a lowercase letter
(?=.*[A-Z]) - A positive look-ahead to require an uppercase letter
(?=.*[0-9]) - A positive look-ahead to require a digit
(?=.*[ -/:-#\[-{-~])` - A positive look-ahead to require a special character
.{6,64} - Any character (but a newline), 6 to 64 occurrences
$ - End of string.

Are consider special non-whitespace characters. I think this is complite list:
! " # $ % & ' ( ) * + , - . / :
; < = > ? # [ \ ] ^ _ ` { | } ~
Try this one:
var passReg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!"#$%&'()*+,-.\/:;<=>?\\#[\]^_`{|}~]).{6,64}$/;
Look at back reference for special characters. In character sets chars like \ and ] must be escaped.

Related

How to check is string has both letter and number in javascript

I have string "JHJK34GHJ456HJK". How to check if this string has both letter and number, doesn't have whitespace, doesn't have special characters like # - /, If has only letter or only number didn't match.
I try with regex below, result is true if string is only number or letter.
const queryLetterNumber = /^[A-Za-z0-9]*$/.test("JHJK34GHJ456HJK");
const input= [
'JHJK34GHJ456HJK',
'JHJKAAGHJAAAHJK',
'123456789012345',
'JHJK34 space JK',
'JHJK34$dollarJK'
];
const regex = /^(?=.*[0-9])(?=.*[A-Za-z])[A-Za-z0-9]+$/;
input.forEach(str => {
console.log(str + ' => ' + regex.test(str));
});
Output:
JHJK34GHJ456HJK => true
JHJKAAGHJAAAHJK => false
123456789012345 => false
JHJK34 space JK => false
JHJK34$dollarJK => false
Explanation:
^ - anchor at beginning
(?=.*[0-9]) - positive lookahead expecting at least one digit
(?=.*[A-Za-z]) - positive lookahead expecting at least one alpha char
[A-Za-z0-9]+ - expect 1+ alphanumeric chars
$ - anchor at end
you should use a regular expression to check for alphanumeric content in the string
/^[a-z0-9]+$/i
The above is a sample that checks for a-z and numbers from 0-9. however, review other options as given here

Regex string contain just digits and *,#,+ in javascript

I am trying to implement a function in Javascript that verify a string.
The pattern i must to do is contain only digits charactor, *, # and +.
For example:
+182031203
+12312312312*#
+2131*1231#
*12312+#
#123*########
I tried alot of thing like
/^[\d]{1,}[*,#,+]{1,}$
but it's doesn't work. I am not sure that i understand good in regex. Please help.
I think you want the pattern ^[0-9*#+]+$:
var inputs = ["+182031203", "+12312312312*#", "+2131*1231#", "12312+#", "#123########"];
for (var i=0; i < inputs.length; ++i) {
console.log(inputs[i] + " => " + /^[0-9*#+]+$/.test(inputs[i]));
}
Using Regex101
/^[0-9\*\#\+]+$/g
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
* matches the character * with index 4210 (2A16 or 528) literally (case sensitive)
# matches the character # with index 3510 (2316 or 438) literally (case sensitive)
+ matches the character + with index 4310 (2B16 or 538) literally (case sensitive)
Try this regular expression:
const rxDigitsAndSomeOtherCharacters = /^(\d+|[*#]+)+$/;
Breaking it down:
^ start-of-text, followed by
( a [capturing] group, consisting of
\d+ one or more digits
| or...
[*#+]+ one ore more of *, # or +
)+, the [capturing] group being repeated 1 or more times, and followed by
$ end-of-text

How to match 2 separate numbers in Javascript

I have this regex that should match when there's two numbers in brackets
/(P|C\(\d+\,{0,1}\s*\d+\))/g
for example:
C(1, 2) or P(2 3) //expected to match
C(43) or C(43, ) // expect not to match
but it also matches the ones with only 1 number, how can i fix it?
You have a couple of issues. Firstly, your regex will match either P on its own or C followed by numbers in parentheses; you should replace P|C with [PC] (you could use (?:P|C) but [PC] is more performant, see this Q&A). Secondly, since your regex makes both the , and spaces optional, it can match 43 without an additional number (the 4 matches the first \d+ and the 3 the second \d+). You need to force the string to either include a , or at least one space between the numbers. You can do that with this regex:
[PC]\(\d+[ ,]\s*\d+\)
Demo on regex101
Try this regex
[PC]\(\d+(?:,| +) *\d+\)
Click for Demo
Explanation:
[PC]\( - matches either P( or C(
\d+ - matches 1+ digits
(?:,| +) - matches either a , or 1+ spaces
*\d+ - matches 0+ spaces followed by 1+ digits
\) - matches )
You can relax the separator between the numbers by allowing any combination of command and space by using \d[,\s]+\d. Test case:
const regex = /[PC]\(\d+[,\s]+\d+\)/g;
[
'C(1, 2) or P(2 3)',
'C(43) or C(43, )'
].forEach(str => {
let m = str.match(regex);
console.log(str + ' ==> ' + JSON.stringify(m));
});
Output:
C(1, 2) or P(2 3) ==> ["C(1, 2)","P(2 3)"]
C(43) or C(43, ) ==> null
Your regex should require the presence of at least one delimiting character between the numbers.
I suppose you want to get the numbers out of it separately, like in an array of numbers:
let tests = [
"C(1, 2)",
"P(2 3)",
"C(43)",
"C(43, )"
];
for (let test of tests) {
console.log(
test.match(/[PC]\((\d+)[,\s]+(\d+)\)/)?.slice(1)?.map(Number)
);
}

Regex uppercase separation but not separating more than 1 next to each other

I have array of values which I have to separate by their uppercase. But there are some cases where the value of the array has 2, 3 or 4 serial uppercases that I must not separate. Here are some values:
ERISACheckL
ERISA404cCheckL
F401kC
DisclosureG
SafeHarborE
To be clear result must be:
ERISA Check L
ERISA 404c Check L
F 401k C
Disclosure G
Safe Harbor E
I tried using:
value.match(/[A-Z].*[A-Z]/g).join(" ")
But of couse it is not working for serial letters.
One option could be matching 1 or more uppercase characters asserting what is directly to the right is not a lowercase character, or get the position where what is on the left is a char a-z or digit, and on the right is an uppercase char.
The use split and use a capture group for the pattern to keep it in the result.
([A-Z]+(?![a-z]))|(?<=[\da-z])(?=[A-Z])
( Capture group 1 (To be kept using split)
[A-Z]+(?![a-z]) Match 1+ uppercase chars asserting what is directly to the right is a-z
) Close group 1
| Or
(?<=[\da-z])(?=[A-Z]) Get the postion where what is directly to left is either a-z or a digit and what is directly to the right is A-Z
Regex demo
const pattern = /([A-Z]+(?![a-z]))|(?<=[\da-z])(?=[A-Z])/;
[
"ERISACheckL",
"ERISA404cCheckL",
"F401kC",
"DisclosureG",
"SafeHarborE"
].forEach(s => console.log(s.split(pattern).filter(Boolean).join(" ")))
Another option is to use an alternation | matching the different parts:
[A-Z]+(?![a-z])|[A-Z][a-z]*|\d+[a-z]+
[A-Z]+(?![a-z]) Match 1+ uppercase chars asserting what is directly to the right is a-z
| Or
[A-Z][a-z]* Match A-Z optionally followed by a-z to also match single uppercase chars
| Or
\d+[a-z]+ match 1+ digits and 1+ chars a-z
Regex demo
const pattern = /[A-Z]+(?![a-z])|[A-Z][a-z]*|\d+[a-z]+/g;
[
"ERISACheckL",
"ERISA404cCheckL",
"F401kC",
"DisclosureG",
"SafeHarborE"
].forEach(s => console.log(s.match(pattern).join(" ")))
function formatString(str) {
return str.replace(/([A-Z][a-z]+|\d+[a-z]+)/g, ' $1 ').replace(' ', ' ').trim();
}
// test
[
'ERISACheckL',
'ERISA404cCheckL',
'F401kC',
'DisclosureG',
'SafeHarborE'
].forEach(item => {
console.log(formatString(item));
});

Regex pattern for replacing insignificant zeros after decimal in a string

I need a regex which replaces all zeros 0 in a string which is after a dot . and does not have any digit 0-9 after it. For example
$2,305.690 ---> $2,305.69, 20,345.690000 % ---> 20,345.69 %, 345.609## ---> 345.609##, 23.000 --> 23, 0.00 --> 0
I tried this (/(?<=\..*)0(?=.*[^0-9].*)/g, '') but it throws an exception, may be because JavaScript does not support Lookbehinds.
Please suggest a JS regex solution.
You can use
\.(\d*?)0+(\D*)$
And replace with .$1$2 if the group 1 length is more than 0 or with $1$2 otherwise. It can be done in the callback function (see below).
The regex matches:
\. - a decimal separator (literal .)
(\d*?) - 0 or more digits (captured group 1 - all digits after the separator up to trailing zeros), as few as possible
0+ - 1 or more zeros...
(\D*) - that are followed by 0 or more non-digits (captured group 2)...
$ - right before the end of string (or line in multiline mode)
var re = /\.(\d*?)0+(\D*)$/gm;
var str = '$2,305.690\n ---> $2,305.69, \n20,345.690 %\n ---> 20,345.69 %, \n345.609##\n ---> 345.609##\n$2,305.000000\n --> $2,305\n0.000\n ---> 0';
var result = str.replace(re, function(m, grp1, grp2) {
return (grp1.length > 0 ? "." : "") + grp1 + grp2;
});
document.write(result.replace(/\n/g, '<br/>'));
You can use
(\.[0-9]+)0+(\D+)
This'll capture decimal values and the rest of the characters including 0 just simply replace using $1$2 and all done
Regex
You could use the following regex.
(\.\d*?[1-9]*)(0+)(\s*%?)$
Here is an example on how to use it:
var number = '$2,305.69000';
number = number.replace(/(\.\d*?[1-9]*)(0+)(\s*%?)$/,'$1$3');
alert(number); //shows $2,305.69
Not a good idea may be but will do the work
var num = '200.00900';
if (num.match(/[.]\d+/)) {//~num.indexOf('.') will fail when num will be '200.'
num = num.replace(/0+\b/g, '');
}
console.log(num);

Categories

Resources