Convert a string to an array without splitting it - javascript

Is it somehow possible to convert a string to an array as easy as possible without any hacks? and without splitting the string at some point?
For example bringing this:
temp = 'Text';
to this:
temp = ['Text'];
It might be a simple question but I couldn't find any solution without splitting the string.

const temp = 'text';
console.log(new Array(temp));
console.log([temp]);
console.log(temp.split());
console.log([].concat(temp));
There are a few options out there.

If you want an array with the string as a single value just create a new array with that string.
temp = [temp];

Related

Array to Newline String to Array again through HTML

I have an array that comes in from from my API that I would like to arrange in a way that is better for the user (namely, in a column as opposed to the typical comma separated printed array).
This is my JS Fiddle to give a clearer picture: https://jsfiddle.net/2z89owas/
My question is, how can I get output3 to display just like output (and maintain its status as an iterable array like it was as dates)?
First you should not be using value for an html element. You can use .value for extracting value from inputs. Change your line to:
var val = document.getElementById('output2').innerHTML;
Afterwards, you have to split the same way you did join.
var dates3 = val.split('<br>');
document.getElementById('output3').innerHTML = dates3;
You can directly use join, something like:
document.getElementById('output3').innerHTML = dates.join(',');
You can try mapping over the contents of dates instead, as so:
let datesElem = dates.map(date =>`<p>${date}</p>`);
// test: console.log(datesElem)
document.getElementById('output3').innerHTML = datesElem

convert a string to object in javascript like regular expression

I have the following variable
var characters = /[-()&]/
and i have a string like this
var characters_string = "/[-()&]/"
this string is result from array and join, because i need to add news variables, so, i need to convert characters_string to a object like characters because is not working with characters_string
because when i use in this line
var filter_d = info.split(/[.,%]/)
and i use it like this
var filter_d = info.split(characters_string)
but i get error
thanks
You can use new RegExp,but you need delete the "/" at the start and the end of the string
new RegExp(characters_string.slice(1, -1))

Dynamic string cutting

Okay, so I have a filepath with a variable prefix...
C:\Users\susan ivey\Documents\VKS Projects\secc-electron\src\views\main.jade
... now this path will be different for whatever computer I'm working on...
is there a way to traverse the string up to say 'secc-electron\', and drop it and everything before it while preserving the rest of it? I'm familiar with converting strings to arrays to manipulate elements contained within delimiters, but this is a problem that I have yet to come up with an answer to... would there be some sort of regex solution instead? I'm not that great with regex so I wouldn't know where to begin...
What you probably want is to do a split (with regex or not):
Here's an example:
var paragraph = 'C:\\Users\\susan ivey\\Documents\\VKS Projects\\secc-electron\\src\\views\\main.jade';
var splittedString = paragraph.split("secc-electron"); // returns an array of 2 element containing "C:\\Users\\susan ivey\\Documents\\VKS Projects\\" as the first element and "\\src\\views\\main.jade" as the 2nd element
console.log(splittedString[1]);
You can have a look at this https://www.w3schools.com/jsref/jsref_split.asp to learn more about this function.
With Regex you can do:
var myPath = 'C:\Users\susan ivey\Documents\VKS Projects\secc-electron\src\views\main.jade'
var relativePath = myPath.replace(/.*(?=secc-electron)/, '');
The Regex is:
.*(?=secc-electron)
It matches any characters up to 'secc-electron'. When calling replace it will return the last part of the path.
You can split the string at a certain point, then return the second part of the resulting array:
var string = "C:\Users\susan ivey\Documents\VKS Projects\secc-electron\src\views\main.jade"
console.log('string is: ', string)
var newArray = string.split("secc-electron")
console.log('newArray is: ', newArray)
console.log('newArray[1] is: ', newArray[1])
Alternatively you could use path.parse(path); https://nodejs.org/api/path.html#path_path_parse_path and retrieve the parts that you are interested in from the object that gets returned.

Getting value from MathQuill

I am using MathQuill I would like to get the values of the fraction I got it using Split which wasn't robust !
var str="\frac{12}{3}+\frac{2}{3}";
How to get the values of a fraction using JavaScript ! I found that I should use regrex ! I am new to javascript regrex ! I want values as
var fra1=12;
var fra2=3;
var fra3=2;
var fra4=3;
If you have to use regex, and you only need it for this particular string, then the simplest method is to use .match and convert to number:
const str = "\frac{12}{3}+\frac{2}{3}";
const frasStrings = str.match(/\d+/g);
const frasNumbers = frasStrings.map(Number);
console.log(frasNumbers);

Remove one line in txt file ( NODE.js)

I make a text file "foo\nbar\nbas"
When i append coke(with adding \n), then the file will be "foo\nbar\nbas\ncoke"
I want to remove the foo.
Help me!
Fo your use case that you have provided, a simple answer is split on \n, remove the first item, add the new item to the end, and join the array to form your new string.
var parts = "foo\nbar\nbas".split("\n").slice(1);
parts.push("coke");
var updated = parts.join("\n");
Other option is to use indexOf to find the first occurrence of \n and substring to select the portion of the string, then it is a simple concatenation.
var str = "foo\nbar\nbas";
var position = str.indexOf("\n")+1;
var updated = str.substring(position) + "\ncoke";
You can use simple string manipulation if your task is not complicated by further constraints. Such is as follows:
var fileContents = "foo\nbar\nbas";
// Read Text File, in this case, I have set it.
fileContents = fileContents.replace("foo\n", "");
fileContents += "\ncoke";
// Write back to file

Categories