Java script number format change [duplicate] - javascript

This question already has answers here:
how to split a number into groups of n digits
(4 answers)
Split large string in n-size chunks in JavaScript
(23 answers)
How can I split a string into segments of n characters?
(17 answers)
Closed 3 years ago.
I've number i.e. 12345678 and I want to change it to the following format
[12,34,56,78]. How can I do that?

You can use match
var str = '12345678';
console.log(str.match(/.{1,2}/g).map(Number));

Related

What can I change in my code to extract the first number? [duplicate]

This question already has answers here:
Get the first integers in a string with JavaScript
(5 answers)
How can I extract a number from a string in JavaScript?
(27 answers)
Closed 1 year ago.
I had a problem with extracting numbers from strings. With all the inputs the code works correctly, but there is a task -
If there are two separate numbers in the string - return the first of them.
So from this string 'only 5 or 6 apples', I should get 5. Not 56 or 5 6. I have no idea what to do.
My code looks like this:
function count(apples) {
const number = Math.floor(Number(apples.replace(/[^0-9.]+/g, '')));
console.log(number);
}

How to get remaining values except matches in JavaScript Regex Match [duplicate]

This question already has answers here:
Split string into array without deleting delimiter?
(5 answers)
JS string.split() without removing the delimiters [duplicate]
(10 answers)
Closed 2 years ago.
String ${provider.name} - ${item.publication_time|date|relative}
Regex /\${([^}]+)}/gmi
From string.match(/\${([^}]+)}/gmi) this, I am getting ["${provider.name}", "${item.publication_time|date|relative}"] as output.
But I need other unmatched parts of the string in the output array. My expected result should be ["${provider.name}", " - ", "${item.publication_time|date|relative}"]
Can you please suggest me how can I achieve this by changing the regex?

Converting a number string to a number? [duplicate]

This question already has answers here:
Strip all non-numeric characters from string in JavaScript
(12 answers)
Closed 5 years ago.
I have a number string:
$1,000,000
Is there an easy way to convert it to a number figure soit can be worked with as a number.
1000000
$ node
> parseInt('$1,000,000'.replace(/,|\$/g,''))
1000000

how to make a list of numbers in a given string, javascript? [duplicate]

This question already has answers here:
get all numbers in a string and push to an array (javascript)
(2 answers)
Closed 5 years ago.
Here is a string: (1 AND 2) OR 30 AND (4 AND 5).
I need a list of numbers included in this string.
Please help me out.
Use match:
var numbers = "(1 AND 2) OR 30 AND (4 AND 5)"
var formatted = numbers.match(/\d+/g)
console.log(formatted); // [1,2,30,4,5]

how to split into character group? [duplicate]

This question already has answers here:
Split large string in n-size chunks in JavaScript
(23 answers)
Closed 9 years ago.
I want to split string
"abcdefgh"
to
"ab","cd","ef","gh"
using javascript split()
"abcdefgh".split(???)
Can you please help ?
Instead of split, try match:
var text = "abcdefgh";
print(text.match(/../g));
prints:
ab,cd,ef,gh
as you can see on Ideone.

Categories

Resources