Javascript: Split File into an Array - javascript

I have a list of english words in a text file. I would like to create an array from this file separated by words:
var dictionaryWords = ["Apple","Orange","Banana","Strawberry"]
How can I use do this in javascript? and please try to explain in beginner terms since I'm still new to this! Thanks!

If you have the file contents in a variable, you use the split() method to split it into an array:
var dictionary = file_contents.split("\n");
\n is the newline character.
You can use AJAX to read the file from the server into a Javascript variable. There are many AJAX tutorials on the web, I'm not going to try to teach that here.

Related

Looking to see how I can split a string into two parts, while keeping full words intact

As the title says, looking to see how I can split a string into two parts, while keeping full words intact, and having access to both parts with JavaScript. I am using this string in a pug file.
For instance, I have a very long string
let str = `hello i am a very long string that needs to be split into two different parts! thank you so much for helping.'
I want the first 70 characters, to use as a headline, then the rest to use for the body of the element.
I have used this regex that I found on an older post that gives me the first 70+ characters, with full words...
str.replace(/^([\s\S]{70}[^\s]*)[\s\S]*/, "$1")
which does what I want it to do. But now I need to figure out if there is a way to get the rest of that string?
str1 = `hello i am a very long string that needs to be split into two different`
str2 = `parts! thank you so much for helping.`
Again, using this in a pug file, so if there is a simple answer, I would love that. Otherwise I suppose I could just write a function and import the script to the pug view.
react code

Javascript easy array declartion option like perl

In perl we can declare the array with qw or quote word take make each word is taken into individual array cell.
eg.
my #arr= qw( hello
world)
or else you need to quote each word
eg
my #arr = ("hello" , "word");
Is there something similar in javascript as sometime it need lot of formatting to simple declare array.
This is what you need, for this specific case: const arr = 'hello world'.split(' ');.
Edit:
Check out the docs for String.split on MDN. Also, read something on types in JavaScript, if you are wondering why it is possible to call this method on string literal, as I did.

Replace a string's special characters using regex

the string looks something like below:
/1/2/3/4 however I want to replace this with ?1=2&3=4.
I am planning to use REReplace in ColdFusion.
Could you suggest me a regex for this ?I also thought of using loops but stuck either way...
Thanks in Advance
A bit cumbersome without making it more manageable using a loop as #Leigh suggested; but you can use the following on string inputs that contain even occurrences of n/m in the format you described:
var s = "/1/2/3/4/5/6";
s.replace(/^\//,'?').replace(/(\d+)\/(\d+)/g,'$1=$2').replace(/\//g,'&')
// => "?1=2&3=4&5=6"

How to best parse a text file using .split in Javascript

I'm having problems using split to try and parse a text file.
the text file is as shows:
123.0 321.02
342.1 234.03
425.3 326.33
etc. etc.
When I read using a FileReader() and doing a readAsText call on the file, the file appears in a string as such:
"123.0 321.02\r\n342.1 234.03\r\n ..." (How it appears in Firebug)
Currently I'm trying to split it like this:
var reader = FileReader();
reader.readAsText(f);
alert(reader.result);
var readInStrings = reader.result.split(/|\s|\n|\r|/);
but when I do this, the resulting array has values as shown:
["123.0", "321.02", "", "342.1", "234.03", "" etc....]
Can anyone explain to me where the values of {""} in the array are coming from and how to correctly split such a file as to only get the number strings as the values?
Any help would be greatly appreciated, thanks!
Note*: Currently doing this in javascript
This is likely due to splitting on each newline and carriage return character rather than each bundle of such characters. To prevent this issue, you could cluster them in the regular expression such as /\s+/ or something similar.

Binary Converter For Loop Trouble

So I am making a binary converter for science project and I am having a bit of difficulty.
As of now, I am trying to write a for loop that gets each individual variable of the user's input into a seperate variable.
However, I can't find a way to only create the same number of variables as the length of the string provided, so I can only use document.write, which is not what I want.
Can anyone help me figure out how to store each character of a string into a seperate variable and post that solution here please?
Thanks for your consideration.
Strings are array-like objects. They expose a length property and support array indexers to access the individual characters of the string. You can simply use a for loop to iterate through the characters.
var myString = "Hello World";
for(var i=0; i<myString.length; i++) {
var character = myString[i];
...
}

Categories

Resources