regex to match a string that starts from 100 to 300 - javascript

I have several strings like
I need to match the strings that start wih >=100 and <=300 folowed by space and then any string.
The expected result is
I have tried with
[123][0-9][0-9]\s.*
But this matched incorrectly giving 301, 399 and so on. How do I correct it?

If you're absolutely set on a regex solution, try looking for 100 - 299 or 300
const rx = /^([12][0-9]{2}|300)\s./
// | | | | | | |
// | | | | | | Any character
// | | | | | A whitespace character
// | | | | Literal "300"
// | | | or
// | | 0-9 repeated twice
// | "1" or "2"
// Start of string
You can then use this to filter your strings with a test
const strings = [
"99 Apple",
"100 banana",
"101 pears",
"200 wheat",
"220 rice",
"300 corn",
"335 raw maize",
"399 barley",
"400 green beans",
]
const rx = /^([12][0-9]{2}|300)\s./
const filtered = strings.filter(str => rx.test(str))
console.log(filtered)
.as-console-wrapper { max-height: 100% !important; }

That is because in your pattern, it also matches 3xx where x can be any digit and not just 0. If you change your pattern to match 1xx, 2xx and 300 then it will return the result as you intended, i.e.:
/^([12][0-9][0-9]|300)\s.*/g
See example below:
const str = `
99 Apple
100 banana
101 pears
200 wheat
220 rice
300 corn
335 raw maize
399 barley
400 green beans
`;
const matches = str.split('\n').filter(s => s.match(/^([12][0-9][0-9]|300)\s.*/));
console.log(matches);
However, using regex to match numerical values might not be as intuitive than simply extracting any numbers from a string, converting them to a number and then simply using mathematical operations. We can use the unary + operator to convert the matched number-like string as such:
const str = `
99 Apple
100 banana
101 pears
200 wheat
220 rice
300 corn
335 raw maize
399 barley
400 green beans
`;
const entries = str.split('\n').filter(s => {
const match = s.match(/\d+\s/);
return match !== null && +match[0] >= 100 & +match[0] <= 300;
});
console.log(entries);

Related

bitwise operation of string and hex in javascript

How can I do bitwise operation between string and hexadecimal?
I need to use input of string to bitwise OR with hexadecimal and result should be hexadecimal.
First, input is string of time.
So 23:00 would be ["23", "00"].
Then I need it in hexadecimal as it is. For example, 23->0x23, 11->-0x11.
Now, bitwise OR with hexadecimal. But it doesn't give the result I want.
Expected result of (time[0] | 0x80) is 0xA3, where time[0] is 0x23 (0x isn't necessary)
Below is what I coded.
let input = "23:00"
let time = input.split(":"); //--> ["23","00"]
console.log(time[0] | 80); //--> 87
console.log(time[0] | "80"); //--> 87
console.log("0x"+time[0] | 80); //--> 115
console.log("0x"+time[0] | "0x80") //--> 163
you need to use proper radix in string2int2string conversions:
let input = "23:00"
let time = input.split(":"); //--> ["23","00"]
let temp = parseInt(time[0],16) | 0x80;
console.log(temp.toString(10)); // prints 163
console.log(temp.toString(16)); // prints a3

Remove everything after a certain character in txt file

hello ı m new to coding , ı need your help about something , ı m trying to filter a txt file and make a list in a format i like
here is what original txt file looks like :
pepitbeng:davy141089 | LV: 5 | BE: 1017 | RP: 400 | Refunds: 3 | Champs: 1 | Skins: 0 | Email Verified: true | Lastplay: Error
korvin918:M5al3elu2z6k | LV: 41 | BE: 2065 | RP: 23 | Refunds: 1 | Champs: 57 | Skins: 23 | Email Verified: true | Lastplay: 1/11/2019 7:02:15 PM
monkeyshadowtms:apolo2002 | LV: 21 | BE: 6795 | RP: 0 | Refunds: 3 | Champs: 10 | Skins: 0 | Email Verified: true | Lastplay: 7/25/2019 5:00:15 PM
and there are thousands of them
what ı like to do is line by line delete everything after space so only id and password left , end result will looks like this
pepitbeng:davy141089
korvin918:M5al3elu2z6k
monkeyshadowtms:apolo2002
ı try few things but can only get first line ,
var fs = require('fs');
var textByLine = fs.readFileSync('1.txt').toString().split(" ");
console.log(textByLine[0]);
this way ı can get pepitbeng:davy141089 but cant get to second line because everything is deleted after them so how can ı get 0 array of every line
ı also try this
var fs = require('fs');
var textByLine = fs.readFileSync('1.txt').toString().split("\n");
console.log(textByLine[0]);
this way ı can get line by line but whole part result of above code
pepitbeng:davy141089 | LV: 5 | BE: 1017 | RP: 400 | Refunds: 3 | Champs: 1 | Skins: 0 | Email Verified: true | Lastplay: Error
ı feel like ı should use forEach() function but ı dont know how to implement to this waiting for your response thanks.
sorry to bother ı manage to solve
var fs = require('fs');
var textByLine = fs.readFileSync('1.txt').toString().split("\n");
console.log(textByLine[0].split(" ")[0]);
hopefully help someone else
You can use a regular expression to match non-spaces at the beginning of the line:
var fs = require('fs');
var lines = fs.readFileSync('1.txt')
.toString()
.split("\n")
.map(line => line.match(/\S*/)[0]);
\S matches a non-space character, and the * repeater matches as many of those characters in a row as it can.
const text = `pepitbeng:davy141089 | LV: 5 | BE: 1017 | RP: 400 | Refunds: 3 | Champs: 1 | Skins: 0 | Email Verified: true | Lastplay: Error
korvin918:M5al3elu2z6k | LV: 41 | BE: 2065 | RP: 23 | Refunds: 1 | Champs: 57 | Skins: 23 | Email Verified: true | Lastplay: 1/11/2019 7:02:15 PM
monkeyshadowtms:apolo2002 | LV: 21 | BE: 6795 | RP: 0 | Refunds: 3 | Champs: 10 | Skins: 0 | Email Verified: true | Lastplay: 7/25/2019 5:00:15 PM`;
var lines = text
.split("\n")
.map(line => line.match(/\S*/)[0]);
console.log(lines);
If your output needs to be a string instead, then replace everything past a space with the empty string:
const text = `pepitbeng:davy141089 | LV: 5 | BE: 1017 | RP: 400 | Refunds: 3 | Champs: 1 | Skins: 0 | Email Verified: true | Lastplay: Error
korvin918:M5al3elu2z6k | LV: 41 | BE: 2065 | RP: 23 | Refunds: 1 | Champs: 57 | Skins: 23 | Email Verified: true | Lastplay: 1/11/2019 7:02:15 PM
monkeyshadowtms:apolo2002 | LV: 21 | BE: 6795 | RP: 0 | Refunds: 3 | Champs: 10 | Skins: 0 | Email Verified: true | Lastplay: 7/25/2019 5:00:15 PM`;
const newText = text.replace(/ .*/g, '');
console.log(newText);

"+" before variable in node.js / nuxt [duplicate]

What does the +d in
function addMonths(d, n, keepTime) {
if (+d) {
mean?
The + operator returns the numeric representation of the object. So in your particular case, it would appear to be predicating the if on whether or not d is a non-zero number.
Reference here. And, as pointed out in comments, here.
Operator + is a unary operator which converts the value to a number. Below is a table with corresponding results of using this operator for different values.
+----------------------------+-----------+
| Value | + (Value) |
+----------------------------+-----------+
| 1 | 1 |
| '-1' | -1 |
| '3.14' | 3.14 |
| '3' | 3 |
| '0xAA' | 170 |
| true | 1 |
| false | 0 |
| null | 0 |
| 'Infinity' | Infinity |
| 'infinity' | NaN |
| '10a' | NaN |
| undefined | NaN |
| ['Apple'] | NaN |
| function(val){ return val }| NaN |
+----------------------------+-----------+
Operator + returns a value for objects which have implemented method valueOf.
let something = {
valueOf: function () {
return 25;
}
};
console.log(+something);
It is a unary "+" operator which yields a numeric expression. It would be the same as d*1, I believe.
As explained in other answers it converts the variable to a number. Specially useful when d can be either a number or a string that evaluates to a number.
Example (using the addMonths function in the question):
addMonths(34,1,true);
addMonths("34",1,true);
then the +d will evaluate to a number in all cases. Thus avoiding the need to check for the type and take different code paths depending on whether d is a number, a function or a string that can be converted to a number.

Calculating with xor variable

I do my calculations like this:
117^196
I get:
177
Now what I want to do is to get 117 back so I need to make a replace
(replace)^196 = 117
Whats the opposite operation from the xor operator?
The opposite of xor is xor :). If you xor something twice (a^b)^b == a.
This is relatively easy to show. For each bit:
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
Doing this on any pair of numbers a,b, it's easy to see that
a^b xor'd by either a or b yields the other (xor a yields b, and vice versa)
1 2 filter result
0^0^0 = 0
0^1^0 = 1
0^1^1 = 0
1^0^0 = 1
1^0^1 = 0
1^1^1 = 1
it's just xor it self.
like +'s opposite is -
xor's opposite is xor
Just use the result that you got: 177
117 ^ 196 = 177 | () ^ 196
117 ^ 196 ^ 196 = 177 ^ 196 | self-inverse
117 ^ 0 = 177 ^ 196 | neutral element
117 = 177 ^ 196
XOR has three important properties. It is
associative
commutative
self-inverse
This means that a value is its own inverse:
a^a = 0
Since it is also both commutative and associative, you can rearrange and xor-expression containing an event amount of the same operands like this:
a^O^b^c^O^d = O^O^a^b^c^d = 0^a^b^c^d = a^b^c^d
You could say that operands that appear an even amount of time "cancel each other out".

What is the purpose of a plus symbol before a variable?

What does the +d in
function addMonths(d, n, keepTime) {
if (+d) {
mean?
The + operator returns the numeric representation of the object. So in your particular case, it would appear to be predicating the if on whether or not d is a non-zero number.
Reference here. And, as pointed out in comments, here.
Operator + is a unary operator which converts the value to a number. Below is a table with corresponding results of using this operator for different values.
+----------------------------+-----------+
| Value | + (Value) |
+----------------------------+-----------+
| 1 | 1 |
| '-1' | -1 |
| '3.14' | 3.14 |
| '3' | 3 |
| '0xAA' | 170 |
| true | 1 |
| false | 0 |
| null | 0 |
| 'Infinity' | Infinity |
| 'infinity' | NaN |
| '10a' | NaN |
| undefined | NaN |
| ['Apple'] | NaN |
| function(val){ return val }| NaN |
+----------------------------+-----------+
Operator + returns a value for objects which have implemented method valueOf.
let something = {
valueOf: function () {
return 25;
}
};
console.log(+something);
It is a unary "+" operator which yields a numeric expression. It would be the same as d*1, I believe.
As explained in other answers it converts the variable to a number. Specially useful when d can be either a number or a string that evaluates to a number.
Example (using the addMonths function in the question):
addMonths(34,1,true);
addMonths("34",1,true);
then the +d will evaluate to a number in all cases. Thus avoiding the need to check for the type and take different code paths depending on whether d is a number, a function or a string that can be converted to a number.

Categories