Regular Expression doesn't count characters that are part of * quantifier - javascript

the regular expression: /([a-z][0-9]*){6,12}/i
so i am expecting this to return true if a string contains more than 6 and less than 12 characters even if there aren't 6 alphabetical characters, but it doesn't, i want "123456789a" to return true and "abcdefghi1", but the first one doesn't.
var myRegEx = /([a-z][0-9]*){6,12}/i;
function checkIt() {
var myString = document.getElementsByTagName("input")[0].value;
if(myRegEx.test(myString) == true) {
document.getElementsByTagName("p")[0].className = "trueOrFalse true";
document.getElementsByTagName("p")[0].innerHTML = "True";
}
else {
document.getElementsByTagName("p")[0].className = "trueOrFalse false";
document.getElementsByTagName("p")[0].innerHTML = "False";
}
}

https://jsfiddle.net/y71mudms/2/
the correct regex is
var myRegEx = /^[0-9]{6,11}[a-z]{1}$/i;
we search exact match of digits + 1 char from 6 to 12
/^[0-9]{6,11}[a-z]{1}$/i;
^ ^ ^ ^
| | | |_ end of string
| | |
| | |_ from 6 min to 11 max repetitions
| |
| |_ digit 0-9 include all digits
|
|_ begin of string
ref. 6 digits regular expression

Just wrap a check for the string-length around your regex-conditions:
if string.length > 6 && string.length < 12) {
// if regex...
}

Related

how to concatenate bits in javascript?

I want to concatenate the bits of two number values like this:
+---------+---------+---------+-----------------+
| | val1 | val2 | concatenate val |
+---------+---------+---------+-----------------+
| decimal | 18 | 16 | 592 |
| hexa | 0x12 | 0x10 | 0x250 |
| binary | 0b10010 | 0b10000 | 0b1001010000 |
+---------+---------+---------+-----------------+
I have try to just concatenate with + like that :
const test = 0b10010, test1 = 0b10000
console.log(test+test1)//return 34
It does not concatenate the values but adds them together.
You could shift the first value by the bitwise length of the second value before adding.
const
add = (a, b) => (a << Math.ceil(Math.log2(b)) + 1) + b;
test = 0b10010,
test1 = 0b10000,
console.log(add(test, test1)); //

regular expression for ip v4

I am doing an ip v4 validator program with javascript.
The ip has to be between the range of 1.0.0.0 to 255.255.255.255
My problem is that I also get the 0.0.0.0 and should not take it.
I leave you my regular expression:
Var Expression1 = new RegExp ("[0-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-5]) [3] ([0-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] ] | 25 [0-5]) $ ");
Thank you!
Add "not 0.0.0.0" negative lookbehind (^(?!0\.0\.0\.0)) at the beginning of the line:
^(?!0\.0\.0\.0)(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
Demo: https://regex101.com/r/dIkHRJ/2
P.S.
Please notice your regex is incomplete and broken. Use the corrected one from the sample above.
Why not use a function, it will be clearer not using a regex:
function validateIP(ip) {
var p = ip.split('.'); // split it by '.'
if(p.length !== 4) return false; // if there is not for parts => false
for(var i = 0; i < 4; i++) // for each part
if(isNaN(p[i]) || i < 0 || i > 255) return false; // if it's not a number between 0 - 255 => false
return p[3] > 0; // return true if the last part is not 0, false otherwise
}
alert(validateIP(prompt("IP: ")));
The more accurate solution using RegExp.prototype.test(), String.prototype.split() and Array.prototype.every() functions:
var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/,
validateIPv4 = function(ip) {
return re.test(ip) && ip.split('.').every(function(d, idx){
var octet = Number(d);
if (idx === 0) {
return octet > 0;
}
return 0 <= octet && octet <= 255;
});
};
console.log(validateIPv4('0.0.0.0'));
console.log(validateIPv4('1.0.0.0'));
console.log(validateIPv4('127.0.0.1'));
console.log(validateIPv4('255.255.255.255'));

How to insert new line after N char count in javascript?

I have a string which may have new line '\n' char in it. Now I want to insert new line '\n' after every 4 (or N) chars in that string.
For example:
1)
INPUT: "I am John Doe."
OUTPUT: "I am\nJohn\nDoe"
In above example inserting '\n' after 4 char including space
2)
INPUT: "I\nam John Doe"
OUTPUT: "I\nam J\nohn \nDoe"
In above example inserting space after 4 chars after first '\n' already present in the string
3)
INPUT: 12345\n67890
OUTPUT: 1234\n5\n6789\n0
4)
INPUT: "1234\n56\n78901"
OUTPUT: "1234\n56\n7890\n1"
So far I have created a function which inserts '\n' after every 4 chars but it does not consider '\n' if it is already present in the original string.
function addNewlines(str) {
if (str.length >= 4) {
var result = '';
while (str.length > 0) {
result += str.substring(0, 4) + '\n';
str = str.substring(4);
}
return result;
}
return str;
}
I call this function on every keypress and pass the original string and get output and use it further. I hope you understand what I meant to say here. It should preserve the previously inserted new lines.
Let me know I can explain further. With more examples.
Here is my best guess as to what is being asked for :
function addNewLines (str) {
return str.replace (/(?!$|\n)([^\n]{4}(?!\n))/g, '$1\n');
}
Some test strings and their results :
"I am John Doe.", -> "I am\n Joh\nn Do\ne."
"I\nam John Doe", -> "I\nam J\nohn \nDoe"
"12345\n67890", -> "1234\n5\n6789\n0"
"1234\n56\n78901", -> "1234\n56\n7890\n1"
"ABCD\nEFGH\nIJKL", -> "ABCD\nEFGH\nIJKL\n"
"1234", -> "1234\n"
"12341234" -> "1234\n1234\n"
For those of you for whom regular expressions are mysterious here is a breakdown:
---------------------------- (?! Check that following character(s) are not
| ------------------------- $|\n Beginning of string or a newline character
| | --------------------- )
| | | -------------------- ( Start of capture group 1
| | || ------------------ [^\n] Any single character other than newline
| | || | -------------- {4} Previous element repeated exactly 4 times
| | || | | ----------- (?! Check that following character(s) are not
| | || | | | --------- \n a newline
| | || | | | | ------- )
| | || | | | | |------ ) End of capture group 1
| | || | | | | || ---- /g in replace causes all matches to be processed
| | || | | | | || |
/(?!$|\n)([^\n]{4}(?!\n))/g
function parseInput(str, char, length){
var split = str.split(char),
regex = RegExp('(.{' + length + '})','g');
split[split.length-1] = split[split.length - 1].replace(regex, '$1' + char);
return split.join(char);
}
console.log(parseInput("I am John Doe.", "\n", 4));
// output = "I am\n Joh\nn Do\ne."
Split the string by "\n" str.split("\n"). You get an array of strings.
Do your additional parsing and manipulation checking the element length and putting the result in a new array results.
Join the strings using results.join("\n").
This will remove "\n" duplicates too if you avoid to append or prepend "\n" to results elements.
Here is my code:
function ngram_insert (n, ins, input)
{
var output = "";
var i = 0;
while (i < strlen(input))
{
if (i > 0 && i % n == 0)
{
output += ins;
}
output += input[i];
i++;
}
return output;
}
Test:
var output = ngram_insert (3, "\n", "This is a test.");
function f(n, ins, str) {
if (str.length == 0)
return "";
var i = str.indexOf("\n"), len = str.length, newStr;
if (i == -1) {
i = 1;
newStr = str[0];
}
else {
newStr = str.substring(0, i + 1);
}
var k = 1;
while (k + i < len) {
newStr += str[k + i];
if (k % n == 0) {
newStr += ins;
}
k++;
}
return newStr;
}
Calling f(4, "\n", "I\nam John Doe"); returns "I\nam J\nohn \nDoe"

RegExp: match known number

i want to match a known number(floating point or integer) with a string and finding number of that repeat.
like match 22 in "22.5 98 12 22 322"
how i can do it with regular expression ?
try using this:
if (numbers.indexOf("22") != -1) {
//The number 22 is in the String
}
It's not exactly a regex but why use it when you don't need it?
str.match(/substr/g).length
returns number of occurrences of "substr" in "str". For example:
str = "22.5 98 12 22 322"
num = str.match(/22/g).length
alert(num) // 3
If you want to match integers rather than just substrings, make sure the number is preceded and followed by a space:
str = "22.5 98 12 22 322"
num = str.match(/(^|\s)22($|\s)/g).length
alert(num) // 1
Another option, without regular expressions:
str = "22.5 98 12 22 322"
num = str.split(" ").filter(function(item) {
return Number(item) === 22;
}).length;
If you mean you should find only the numbers that repeat within the same string, you can use something like this:
var test_str = '22 34 55 45345 34 22 555';
var matches = test_str.match(/(\b[0-9]+\b)(?=.+\b\1\b)/g);
// matches contains ["22", "34"], but not "55"
var str = "22.5 98 12 22 322";
var pattern = /22/g;
if (pattern.test(str)) {
// it matches "22", do your logic here...
}
see RegExp and RegExp.test(string) for more
Corrolating integer/floating point numbers from text to regex is
very tricky, since regex has no such concept.
With dynamic input, all the work is constructing the regex programatically.
This example doesn't include scientific notation, but it could.
Pseudo code:
If the input matches this parse regex
/^ (?=.*\d) // must be a digit in the input
([+-]?) // capt (1), optional +/-
[0]* // suck up all leading 0's
(\d*) // capt (2), optional digits, will start with [1-9]
([.]?) // capt (3), optional period
(\d*?) // capt (4), non-greedy optional digits, will start with [1-9]
[0]* // suck up all trailing 0's
$/x
sign = '[+]?';
If !length $2 AND !length $4
sign = '[+-]';
Else
If $1 == '-'
sign = '[-]';
Endif
Endif
whole = '[0]*';
If length $2
whole = '[0]*' + $2;
Endif
decfrac = '[.]? [0]*';
If length $4
$decfrac = '[.] ' + $4 + '[0]*';
Endif
regex = '/(?:^|\s)(?=\S*\d) ' + "$sign $whole $decfrac" + ' (?:\s|$)/x';
Else
// input is not valid
Endif
This is a Perl test case.
The dynamic regex produced has not been tested,
I asume it works but I could be wrong.
#samps = (
'00000.0',
'+.0',
'-.0',
'0.',
'-0.00100',
'1',
'+.1',
'+43.0910',
'22',
'33.e19',
);
for (#samps)
{
print "\nInput: $_\n";
if( /^ (?=.*\d) ([+-]?) [0]*(\d*) ([.]?) (\d*?)[0]*$/x ) # 1,2,3,4
{
my ($sign, $whole, $decfrac);
$sign = '[+]?';
if ( !length $2 && !length $4) {
$sign = '[+-]';
} elsif ($1 eq '-') {
$sign = '[-]';
}
$whole = '[0]*';
if ( length $2) {
$whole = '[0]*'.$2;
}
$decfrac = '[.]? [0]*';
if ( length $4) {
$decfrac = '[.] '.$4.'[0]*';
}
my $regex = '/(?:^|\s)(?=\S*\d) '. "$sign $whole $decfrac" . ' (?:\s|$)/x';
print "Regex: $regex\n";
}
else {
print "**input '$_' is not valid\n";
next;
}
}
Output
Input: 00000.0
Regex: /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x
Input: +.0
Regex: /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x
Input: -.0
Regex: /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x
Input: 0.
Regex: /(?:^|\s)(?=\S*\d) [+-] [0]* [.]? [0]* (?:\s|$)/x
Input: -0.00100
Regex: /(?:^|\s)(?=\S*\d) [-] [0]* [.] 001[0]* (?:\s|$)/x
Input: 1
Regex: /(?:^|\s)(?=\S*\d) [+]? [0]*1 [.]? [0]* (?:\s|$)/x
Input: +.1
Regex: /(?:^|\s)(?=\S*\d) [+]? [0]* [.] 1[0]* (?:\s|$)/x
Input: +43.0910
Regex: /(?:^|\s)(?=\S*\d) [+]? [0]*43 [.] 091[0]* (?:\s|$)/x
Input: 22
Regex: /(?:^|\s)(?=\S*\d) [+]? [0]*22 [.]? [0]* (?:\s|$)/x
Input: 33.e19
**input '33.e19' is not valid
Here's a good tool for you to explore regular expressions :)
http://www.regular-expressions.info/javascriptexample.html
Adopted from the site above:
function demoShowMatchClick() {
var re = new RegExp("(?:^| )+(22(?:$| ))+");
var m = re.exec("22.5 98 12 22 322");
if (m == null) {
alert("No match");
} else {
var s = "Match at position " + m.index + ":\n";
for (i = 0; i < m.length; i++) {
s = s + m[i] + "\n";
}
alert(s);
}
}
This will give you all matches and requires the number to be seperated by beginning or end of string or a whitespace.

Understanding JavaScript bitwise NOT operator and toString() function

Thanks to everyone in advance:
alert((~1).toString(2));
This outputs: -10
But in PHP/Java it outputs 11111111111111111111111111111110
Am I missing something? Why does Javascript add a "-" to the output?
I know Java uses two's complement to represent negative numbers, and 11111111111111111111111111111110 in binary, which is what ~1 gives, represents -2. Or, represented in binary with a negative sign, -10, which is what you got.
The way you calculate the negative of 10 (in base 2) using two's complement is that you first invert all of the bits, giving you:
11111111111111111111111111111101
then you add 1, giving you:
11111111111111111111111111111110
I guess the same is happening in Javascript.
You can use the shift operator >>> to convert the number to an unsigned integer before converting to binary:
(~1 >>> 0).toString(2) // "11111111111111111111111111111110"
Short answer:
A bitwise NOT (~1) performs a 1's complement conversion of the
decimal which gives us -2
The .toString() function basically takes the decimal without the sign 2, converts it to binary 10 and adds a - sign which gives us -10.
A more detailed answer:
It's in the function .toString(). When you output a number via .toString():
If the numObj is negative, the sign is preserved. This is the case
even if the radix is 2; the string returned is the positive binary
representation of the numObj preceded by a - sign, not the two's
complement of the numObj.
Taken from the developer.mozilla.org we got this formula that calculates the 1's complement of an integer, this is used when you perform a NOT (~) on a decimal:
Bitwise NOTing any number x yields -(x + 1). For example, ~5 yields
-6.
Maybe it's better explained with this table and an example:
+-------------------------+-----+-----+-----+-----+-----+-----+------+
| Base 10 Integer | -3 | -2 | -1 | 0 | 1 | 2 | 3 |
+-------------------------+-----+-----+-----+-----+-----+-----+------+
| Base 10 1's Complement | 2 | 1 | 0 | -1 | -2 | -3 | -4 |
+-------------------------+-----+-----+-----+-----+-----+-----+------+
| Base 2 | | | | 0 | 1 | 10 | 11 |
+-------------------------+-----+-----+-----+-----+-----+-----+------+
| Result ~x.toString(2) | 10 | 1 | 0 | -1 | -10 | -11 | -100 |
+-------------------------+-----+-----+-----+-----+-----+-----+------+
Starting with Base 10 integer "2"
Base 10 integer "2" its 1's Complement is "-3". This is the same as performing a NOT (~)
.toString function take the unsigned value (= "3" in base 10 and = "11" in base 2)
.toString function adds a "-" symbol
.toString outputs "-11"
This assumes that you are working in 32 bits...
var valueToNot = parseInt("11110000", 2);
var notResult = 0xFFFFFFFF - valueToNot;
console.log(notResult.toString(2));
results in
11111111111111111111111100001111
Here's a solution to implement NOT in javascript. It ain't pretty but it works.
// Since ~ is the two's complement, then the one's complement is ~(num -1).
var num = 9;
num.toString(2); //returns 1001
~(num - 1).toString(2); //returns -1001
// WHAT the hell?? I guess the negative sign acts as a sign bit.
If you want to view the Binary String of a decimal after a NOT (bit Toggle), then use the following code.
// Programer: Larry Battle
// Purpose: Provide a bit toggle function for javascript.
var getStrCopy = function (str, copies) {
var newStr = str;
copies = (copies > 0) ? copies : 1;
while (--copies) {
newStr += str;
}
return newStr;
};
var convertDecToBase = function ( dec, base, length, padding ) {
padding = padding || '0' ;
var num = dec.toString( base );
length = length || num.length;
if (num.length !== length) {
if (num.length > length) {
throw new Error("convertDecToBase(): num(" + num + ") > length(" + length + ") too long.");
}
num = getStrCopy( padding, (length - num.length)) + num;
}
return num;
};
var formatBinaryStr = function( str ){
return str.replace( /\d{4}/g, '$& ' ).replace( /\s$/,'');
};
var toggleBits = function( dec, length, doFormat ){
length = length || 8;
var str = convertDecToBase( dec, 2, length || 8 );
var binaryStr = str.replace( /0/g, 'o' ).replace( /1/g, '0').replace( /o/g, '1' );
return ( doFormat ) ? formatBinaryStr( binaryStr ) : binaryStr ;
};
// The following requires Firebug or Google Chrome Dev Tools
clear();
console.log( toggleBits( 1 ) ); // returns "11111110"
console.log( toggleBits( 2 ) ); // returns "11111101"
console.log( toggleBits( 50, 16 ) );// returns "1111111111001101"
console.log( toggleBits( 15, 8, true ) ); // returns "1111 0000"
console.log( toggleBits( 520, 16, true ) ); //returns "1111 1101 1111 0111"

Categories