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
Related
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:
Pad a number with leading zeros in JavaScript [duplicate]
(9 answers)
Closed 4 years ago.
guys.
I'm having some issues with this function
const incrementString = str => {
if (!str.match(/[\d+]$/)){
return str += 1
} else{
return str.replace(/[\d+]$/, ch => new Number(ch) + 1)
}
}
What I'm trying to do with that function is + 1 the number at the end of the string, and if the string doesn't has one, I'll add a 1 at the end.
string expected
"foobar000" "foobar001"
"foo" "foo1"
"foobar025" "foobar026"
I don't know if it's possible to do it with replace and regex, I have in mind a solution with loops, .length, split, etc..., but I want to do it with regex, if it's possible.
Problem: How can I take the number at the end of the string, with the leading zeros, and sum them a 1?
this are some examples of the bad behavior of my function
Expected: 'foobar011', instead got: 'foobar11'
Test Passed: Value == 'foo1'
Expected: 'foobar002', instead got: 'foobar2'
Test Passed: Value == 'foobar100'
Test Passed: Value == 'foobar100'
Test Passed: Value == '1'
Thanks and happy holydays
You could store the length of the numerical string and apply after incrementing the wanted leading zeroes.
function increment(s) {
var [left, right = '0'] = s.split(/(\d*$)/),
length = right.length;
return left + (+right + 1).toString().padStart(length, '0');
}
console.log(['foobar000', 'foo', 'foobar025'].map(increment));
I used all your hints and answers to check different options to solve my problem.
Finally, I solved with the "String of numbers" and the padStart hints.
const incrementString = str => {
if (str.match(/[0-9+]+/g)) {
let numbers = str.replace(/[a-z+]+/g, "")
return str.replace(numbers, (Number(numbers) + 1 + '').padStart(numbers.length, 0))
} else {
return str + 1
}
}
I hope this helps others as it helped to me.
Thanks and happy holydays
This question already has answers here:
JavaScript equivalent to printf/String.Format
(59 answers)
Closed 5 years ago.
I have a number, for example:
25297710.1088
I need to add a bit between them and leave two characters after the point.
For example:
25 297 710.10
While I stopped at this:
$(td).text().reverse().replace(/((?:\d{2})\d)/g, '$1 ').reverse());
String.prototype.reverse = function() {
return this.split('').reverse().join('');
}
From this code I get the following:
25 297 710.1 088
Where $(td).text() I get a number from the cell of the row in the table.
If I have numbers, for example:
25297710.10
then i get:
25 297 710.10
It's ok.
What I need to do to leave two characters after the point?
You can use a RegExp to format the number/string. The input is converted to string using the relevant toString method.
function formatNumber(input) {
return input.toString().replace(/\d*(\d{2})(\d{3})(\d{3})\.(\d{2})\d*$/, "$1 $2 $3.$4");
}
var str = "25297710.1088";
var num1 = 25297710.1088;
var num2 = 2545454545454.2254;
var num3 = 232545454511112.3354122313123123;
console.log(formatNumber(str));
console.log(formatNumber(num1));
console.log(formatNumber(num2));
console.log(formatNumber(num3));
I think you can do next steps:
1) you have 25 297 710.10
2) you find position of dot symbol ->#pos
3) you replace bits in string in range between #pos and end of your string
4) you cut string after dot to 2 characters
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:
Closed 10 years ago.
Possible Duplicate:
how to print number with commas as thousands separators in Javascript
I have a function that will add thousand seperators to a number, however it is not working well when a decimal is passed in:
function thousandSep(val) {
return String(val).split("").reverse().join("")
.replace(/(.{3}\B)/g, "$1,")
.split("").reverse().join("");
}
If I pass in 10000, I get 10,000 as expected.
However, passing in 10,000.00 I get 1,000,0.00.
How can I modify the function to handle decimals?
Don't use ., use \d
function thousandSep(val) {
return String(val).split("").reverse().join("")
.replace(/(\d{3}\B)/g, "$1,")
.split("").reverse().join("");
}
function format(n, sep, decimals) {
sep = sep || "."; // Default to period as decimal separator
decimals = decimals || 2; // Default to 2 decimals
return n.toLocaleString().split(sep)[0]
+ sep
+ n.toFixed(decimals).split(sep)[1];
}
format(4567354.677623); // 4,567,354.68