This question already has answers here:
Enhancing regex of thousands separator?
(5 answers)
Closed 10 months ago.
I need a regular expression for adding a dot as a thousand and millon separator in Javascript. I've searched the whole web without finding the format I need.
It would return this:
1
12
123
1.234
12.345
123.456
1.234.567
12.345.678
123.456.789
I'm trying to use it in an input.
handleIdChange = (personalIdNumber) => {
this.validator.updateField("personalId", {
number: personalIdNumber
})
this.props.setVisitorField({
field: "personalId.number",
value: personalIdNumber
});
<CLTextInput
highContrast={true}
onChangeText={this.handleIdChange}
keyboardType="numeric"
placeholder="NĂºmero"
returnKeyType="next"
autoCapitalize="none"
autoCorrect={false}
value={visitor.personalId.number}
error={this.shouldShowError("personalId")}
accessibility
/>
Don't use regular expressions to format numbers. Use a proper number formatter.
If you want german-style number formatting, set that as the locale.
const formatter = new Intl.NumberFormat('de-DE');
for (let i = 0; i < 10; i++) {
const number = 10 ** i;
console.log(formatter.format(number));
}
Related
This question already has an answer here:
Multiply regex matches and replace them in string
(1 answer)
Closed 2 years ago.
For example,
x: My shop of 23 items came to 54
turns into
My shop of 46 items came to 108
where x is a string
Just use replace() with regular expression:
const string = 'My shop of 23 items came to 54';
const doubled = string.replace(/\d+/g, n => n * 2);
// My shop of 46 items came to 108
You can use a custom replace function along with a regex to find the items you want to replace, like so:
const string = `My shop of 23 items came to 54`;
const out = string.replace(/\d+/g, function() {
return parseInt(arguments[0]) * 2;
});
console.log(out);
This question already has answers here:
Regular expression for matching HH:MM time format
(22 answers)
Closed 3 years ago.
I'm using some masked input plugin, which supports mask as the RegEx array. In my case, I want to validate 24h time format (hh:mm). I'm using now RegEx like this:
[/^([0-2])/, /([0-9])/, ':', /[0-5]/, /[0-9]/]
First 2 array elements represents hours
/^([0-2])/, /([0-9])/
Any suggestions how to validate second hour digit? Cause now 25:00 is valid but shouldn't. Need somehow to check first number and if it's 2 - replace [0-9] with [0-3].
Something like this would be more flexible
const arr = ["07:00", "25:05", "23:59", "00:00"];
const valid = arr.filter(t => {
const parts = [hh, mm] = t.split(":");
return !isNaN(hh) && +hh >= 0 && +hh < 24 && !isNaN(mm) && +mm >= 0 && +mm < 60;
});
console.log(valid.length, arr.length, valid.length === arr.length)
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I expect true return value that match condition below :
Only binary number (0 or 1)
Length not more than 8
I'm new to regex, i have googling and read JS RegExp from https://www.w3schools.com/Js/js_regexp.asp but still i don't get it.
I've tried
/[0-1]$/
but still didn't match the condition above
I expect boolean return from regex test if there is contain no other number except 0 or 1 and length not more than 8.
data: {
binRegExp: /[0-1]$/,
isBinary: false,
binNum: '', // get value from user input
},
computed: {
inputCheck(){
return this.isBinary = this.binRegExp.test(this.binNum)
}
}
code above is vue js
If there is solution, please answer below. Thank you
this will takes 1 to 8 [1-0]
const regex = /^[0-1]{1,8}$/
const text = "1010010"
console.log(regex.test(text));
Try
/^[01]{1,8}$/
let strings = ['11110001','1111000111','11112001'];
strings.forEach(x=> /^[01]{1,8}$/.test(x) ? console.log(x,'pass') : console.log(x,'not pass'))
This question already has answers here:
Password REGEX with min 6 chars, at least one letter and one number and may contain special characters
(10 answers)
Closed 5 years ago.
I know this has been asked a million times, but I just can't seem to crack it.
I have this:
function checkPassword(strPassword)
{
var objPattern = new RegExp("^.*(?=.{6,})(?=.*[a-z])[a-z0-9]*$");
var blnResult = objPattern.test(strPassword);
return(blnResult)
}
...but it only seems to check the length, and not if there's a number?
What have I missed?
Edit:
The number can be anywhere in the string, not necessarily at the end.
Keep it simple: if(strPassword.length >= 6 && /\d/.test(strPassword)) will do the work and is way more readable
If you need exactly 6 characters plus 1 number then you can use ^[A-z]{6}[0-9]{1}$ or like atleast 6 characters and atleast 1 number then use ^[A-z]{6,}[0-9]{1,}$
You can just include both tests separately in your function:
function checkPassword(strPassword){
var blnResult = /\w{6,}/.test(strPassword)
&& /\d+/.test(strPassword);
return(blnResult)
}
Demo:
function checkPassword(strPassword){
var blnResult = /\w{6,}/.test(strPassword)
&& /\d+/.test(strPassword);
return(blnResult)
}
var passwords = ["zeaezee2reer", "sds2", "ssdsdsdsdsd", "12155"];
passwords.forEach(function(p){
console.log(p+" ::: "+ checkPassword(p));
});
This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Closed 9 years ago.
I can't figure out how to solve the following problem.
I have an array of numbers from 1 to 100.
I need to convert them to strings but to a length of 5.
So, for instance:
1 becomes 00001
2 becomes 00002
3 becomes 00003
4 becomes 00004
etc, etc..
It seems so easy but I cannot find a function. The best I found was .toFixed(n) which is the number of decimal points to use.
Here's a very simple padding function:
function padLeft(str, length, paddingCharacter) {
str = '' + str; //Make sure that we convert it to a string if it isn't
while (str.length < length) {
str = paddingCharacter + str; //Pad it
}
return str;
}
padLeft(123, 5, '0'); //00123