Explode string without separators [duplicate] - javascript

This question already has answers here:
How to get character array from a string?
(14 answers)
Closed 7 years ago.
I have a string like 12345 and I need to explode it to [1,2,3,4,5]. Normally, I would use split, but there are no separators in here. I could use substr (getting length, making loops), but I will probably have a lot of those strings, so it may not be good for performance. What can I do?

Simply use an empty string as a delimiter:
"12345".split('');

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?

Replace dot in a number [duplicate]

This question already has answers here:
Removing everything except numbers in a string
(7 answers)
Closed 3 years ago.
I want to replace the dot in a number in Javascript with regular expression; if country_temp is, for example, 1.234, how can I put the value 1234 in country_temp2?
I have tried the following:
const country_temp2 = country_temp.replace(/[a-zA-Z]*\**\s*\./g, "")
but it's not working.
Edit: the original string is composed by characters, asterisk, a number and a dot
Sorry, but I have written in a very fast way.
Try this:
country_temp.replace(/\./g,'')

extracting text between two characters [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 6 years ago.
Having a string like:
"*this* not that"
I can select *this*
\*(.*?)\*
but I'm not able to get only this.
what I am trying to achieve is to replace -this- by a new string. What's the easiest way to do that ?
you can try:
"*this* not that".replace(/\*.*\*/,'*new_string*');
//"*new_string* not that"

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

split a string to array containing pairs of two [duplicate]

This question already has answers here:
How can I split a string into segments of n characters?
(17 answers)
Closed 9 years ago.
I have a string like this: 56f7gg5cx887r7gt8r6t7.
Besides splitting it into an array of one and then looping by two i+2 and creating another array with entries containing two by two.
Is there a simpler way?
The result should be like this: ['56','f7','gg','5c','x8','87','r7','gt','8r','6t','7'].
You can use match:
'56f7gg5cx887r7gt8r6t7'.match(/(..?)/g)

Categories

Resources