Javascript regex to match equal number of consecutive 1 and 0 [duplicate] - javascript

This question already has answers here:
Recursive matching with regular expressions in Javascript
(6 answers)
Closed 4 years ago.
I want to write a regex which matches following scenarios,
10
1100
111000
11110000
// etc. consecutive 1 count should equal consecutive 0 count.
Is it possible to write a regex for this?
I suppose I could write, 1{n}0{n}, but it didn't work here: https://regex101.com/
May be this is not doable with regex?
I can of course do this with a loop. But I want to know if this is possible using regex.
Thank you!!!!

Theoretically the answer is no.
We can write regular expressions for strings that belong to regular languages.
Your case belong to a context free language.

Related

Regex for password with specific restrictions [duplicate]

This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Regular expression to check if password is "8 characters including 1 uppercase letter, 1 special character, alphanumeric characters"
(14 answers)
Closed 3 years ago.
I'm trying to come up with a regex that validates passwords. The restrictions are as follows:
Must be at least two of the following:
one lowercase [a-z],
one uppercase [A-Z],
one digit [\d],
one special character [!##\$%\^\&*)(+=._-].
must not begin or end with white-space but can contain white-spaces inside,
must be between 7 and 20 characters long.
So far, this is the last version of what I've come up with:
^(?=.{7,20}$)(?:(?=.*[\d!##\$%\^\&*\)\(+=._-])(?=.*[a-z])\S*|(?=.*[A-Z])(?=.*[\d!##\$%\^\&*\)\(+=._-])\S*|(?=.*[A-Z])(?=.*[a-z])\S*|(?=.*[\d)\(+=._-])(?=.*[!##\$%\^\&*\)\(+=._-])\S*)$
This works for all of the above except letting white-spaces inside. I've gone through multiple regex and this is the best one so far (but also the ugliest).
Edit: Thank you for the fast replies. Why these requirements are in place is beside the point. I know passwords would be more secure if all of the above were required. But as not all customers use password managers...
Now, why is this not a duplicate question? Because no other thread requires any two of the above. They simply start with requiring specific two, than adding another one and so on. This needs to be any two conditions.
Hey you can use below regex to fulfill your requirement
^(?=.\d)(?=.[A-Z])(?=.[a-z])(?=.[^\w\d\s:])([^\s]){7,20}$

Issue working with numbers in Javascript [duplicate]

This question already has answers here:
Large numbers erroneously rounded in JavaScript
(6 answers)
Closed 5 years ago.
I'm new in developing javascript apps, i have a doubt about a behaviour that i'm going to try to explain.
If i execute:
Number(5555555555555555);
Result: 5555555555555555
But if i execute:
Number(55555555555555555);
Result: 55555555555555550
Anybody can explain to me what is the reason of this? Thanks!!
If you need to work with such big numbers I would suggest you use some of the big integer libraries such as this. The reason this happens as far as I know is the way processors and memory work. It's no related to some "bug" in JS.
Integers (numbers without a period or exponent notation) are accurate up to 15 digits. Javacript simply adds zeros to keep the number accurate in terms of its digit length.
Documentation

Small regex issue with validating phone numbers (Javascript) [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 6 years ago.
Wanting to validate phone numbers with the following criteria.
-Minimum of 6 digits.
-Can only have the following symbols "+", "(", ")", "-".
-Contain no more than n consecutive symbols, but numbers are OK.
Here are some examples of what i consider valid:
07519767576
+447519767576
(02380) 346450
(+44) 7519767576
I have been trying to do this myself for quite a while but hitting a brick wall. Here is what i have tried so far
^(?=.{9,}$)(?=[^0-9]*[0-9])(?:([\d\s\+\(\)\-])\1?(?!\1{5}))+?$
This kinda works but its a bit of a hack because it also limits amount of consecutive numbers.
I am not able to do this check in PHP, it has to be done in JS sadly. Is this even possible without needing a degree in regex?
At least one of your requirements is beyond what traditional regular languages in general can do. As pointed out in the comments, counting the number of digits across patterns, groups or regular expressions is not possible in traditional regular languages, which essentially use Deterministic Finite Automata (also knows as DFAs) to compute regular expression matches.
PCRE compatible regular expressions, which is what most languages like Javascript and Python for example, support add additional functionality with things such as backtracking, look ahead matching, grouping, counting for a single group, and so on.
These enhance the set of patterns PCRE regular expressions can match, or more technically the set of languages the expression will accept. But to the best of my knowledge, none of these extensions let one do counting in the way you want to here, at least directly.
Turns out PCRE compatible regular expressions are NP-Complete in theory, but that doesn't mean it's easy or even feasible to write a regular expression for a given problem.
In most cases one would write a small hand rolled parser in a turing complete programming language, which can do what you need fairly easily.
OP mentioned that doing this is not an option and thus the problem as has come to a standstill.

encrypting / shortening a long string in JS [duplicate]

This question already has answers here:
String compression in JavaScript
(8 answers)
Closed 6 years ago.
I need to shorten a long string with a variable length and "decode" it back later on.
The string will be built up like this 0011010011 ........ etc.
My problem right now is that the string will be over a thousand characters long which is far far too long to easily copy and paste around.
Any ideas on how to do this?
read more
Javascript doesn't support binary data by default. I would either use an exisiting package such as binstring or write your own encoding function

What ASCII characters are valid for Javascript variable names? [duplicate]

This question already has answers here:
What characters are valid for JavaScript variable names?
(12 answers)
Closed 7 years ago.
I've seen this other question, unfortunately the answers spend too much attention on Unicode and say nothing about normal ASCII characters.
I need to know which 7-bit ASCII characters (0..127) are valid in a Javascript identifier name.
According to this article, this is all about javascript variables :
The general rules for constructing names for variables (unique identifiers) are:
Names can contain letters, digits, underscores, and dollar signs.
Names must begin with a letter
Names can also begin with $ and _
Names are case sensitive (y and Y are different variables)
Reserved words (like JavaScript keywords) cannot be used as names
You can use all ASCII codes. but you have to comply with above rules.

Categories

Resources