how to split into character group? [duplicate] - javascript

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.

Related

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?

Java script number format change [duplicate]

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));

Replacing “index” array string using Javascript regex [duplicate]

This question already has answers here:
Regex using javascript to return just numbers
(14 answers)
Closed 6 years ago.
with the following code can get the 1 in the string.
var match = /[0-9]+/.exec('[1][2]');
console.log(match);
How do i get the 2 and not the 1 ?
Try with this regex
console.log(/(\[[0-9]\])+/.exec('[1][2]'));
I think it's only a matter of escaping the square brackets

How do I get the last character with jquery [duplicate]

This question already has answers here:
How to get the last character of a string?
(15 answers)
Closed 8 years ago.
I have a string I am trying to get the last character with jquery. How would I do that?
var stringTemp = "fieldNameWithIndex_1";
stringTemp.charAt(stringTemp.length-1);
or
stringTemp.slice(-1);
Use .split():
stringTemp.split('_')[1]

JavaScript / jQuery: how to split string with multiple values [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method

Categories

Resources