I am trying to convert a Java program I wrote into JavaScript.
One of the functionalities of the program include importing a text file that has about a thousand words, separated with line breaks between every word.
In Java, importing this text file and putting it in an array variable was easy, since I could use Scanner.nextLine() and for loops. But I have no idea how I am supposed to do this in JavaScript.
Is there an object analogous to Java's scanner in JavaScript? How can I achieve this task?
You can split a string into an array (of strings) easily:
var words; // array of words
var text; // the input text string containing all the words
words = text.split("\n");
If you need help on how to you obtain the text from a file (or a http server) I'll edit my answer as you'll provide mode information in your question.
Using jQuery :
$('#somediv').load('some.txt', function(__textdata){
});
Related
I have searched for way too long, to solve my existing Problem.
So, there is a Software, that can only read csv-files with ISO-8859-1 (ISO-Latin-1) encoding. And If tried alost everything I can find on the web, but nothing worked. I don´t want to change the Text, I want to change the encoding.
I´ve tried working with this lib: https://github.com/inexorabletash/text-encoding Library and the PapaParse Library and much more. But they are just converting the Text so there are weird symbols replace ä,ö,ü and other characters.
The characters in the csv file itself has characters which are not part of the character set which you're encoding into. You might find that the end of lines have a character which does not exist in the character set you want to use. If you open the csv file in a basic text editor or at the dos prompt use Type myFile.csv you will be able to see which charaters there are in a most basic format. Then stip them out and you should have a file that can be converted. Always work on a copy of the original. The easiest way would be to search and replace in a text editor where you replace the unwanted characters with nothing. not even a space. Keep in mind that if the character is part of the csv construct - eg .. a delimiter - then you would want to replace that with the latin equivalent character.
I found it myself, but thank you anyway.
In js :
const uint8array = new TextEncoder(
'windows-1252',
{NONSTANDARD_allowLegacyEncoding: true}
).encode(csv_data);
const blob = new Blob([uint8array])
(For those, who searched the question: csv_data is an String, containing the values from the read csv-file. Make sure to ad ; behind every value to move to the next column and a /n to move to the next line.)
In HTML :
<script>
window.TextEncoder = window.TextDecoder = null;
</script>
<script src="encoding-indexes.js"></script>
<script src="encoding.js"></script>
And you have to download encoding.js and encoding-indexes.js from https://github.com/inexorabletash/text-encoding
We have a translation extraction tool that we've written, that extracts strings that we've marked for translation in TypeScript. The JavaScript tool reads our Typescript files and has a regex like:
fileContent.match(/this.\translate\((.*?));/);
(simplified for readability, this works fine)
The translation method takes 3 parameters: 1. The string to be translated, 2. any variables that might be interpolated, 3. description. The last 2 are optional.
Examples of the implementation:
this.translate('text to translate');
this.translate('long text' +
'over multiple lines');
this.translate(`text to translate with backticks for interpolation`);
this.translate(`some test with a ${variable}`, [variable]);
this.translate(`some test with a ${variable}`, [variable], 'Description');
We need to extract these 3 parameters from text in JavaScript and have issues parsing it. We are currently using a regex to check the first opening string character (' or "`") and trying to match a closing character, but that is hard to do.
I'm currently trying to use eval (the script doesn't run in the browser, but CLI), like this:
function getParameters(text, variables, description){
return {text: text, variables: variables, description: description}
}
toEval = string.replace('this.translate', 'getParameters');
eval(toEval);
Which works perfect if there are no variables, but complains that "variables" not defined, when we pass in variables.
Can anyone suggest a good/better way to deal with this text extraction?
Instead of regex, you can use either babel or webpack to properly parse Javascript (or typescript) and extract all the information.
I have a webpack plugin that works on static strings only, but it should give a good starting point:
https://github.com/grassator/webpack-extract-translation-keys
I'm very new on this and I isn't a pro on this issues!
I'm just here for request for your help because I know this community is the best for this problems!
Actually I'm using a software called "Bulk Rename Utility", it works for bulk rename files with a very useful interface, one of them is the RegEx option where you can rename files thanks to the regular expresion lenguage.
I was wonder if I can convert this code in a JavaScript code, this is because I need to modify various parameters and it could be better if I can use a JavaScript code.
As I told you, I'm very new on this. After checking some videos I learned how to use the RegEx option in this program and the codes I got was:
In the "Match" field of the program I write: (.*) 10-1[A-Z] (.*).
In the "Replace" filed of the program I write: \1_REF_\2
A then, it do the magic!
As I said, I want to know how I can convert this code to a JavaScript code because I have a lot of this codes and I want just one whole code to make a bulk process.
Thanks you so much!
Like this?
const str = 'abc 10-1G def';
const outputStr = str.replace(/(.*) 10-1[A-Z] (.*)/, '$1REF$2');
console.log(outputStr);
You just need to put $1/$2 in place of the captured groups you want in the replaced string.
To insert code, you can put the code between backticks (`) and you'll be able to type *s normally.
I'm trying to parse a text file and store to an array, but I can't seem to get rid of the unneeded characters.
For example, some of the text will be "fi nd" or "job;" or "writ,er"
Right now I'm using
lettersTemp = InputDataLine.match(/[a-zA-Z]['*]/);
to parse the text file, but that obviously isn't working because I'm pulling the entire string and not getting rid of the extra characters. Anyone got some advice or an easier way to do this?
Is the result a string that consists of only letters (without space separating words)? If so you can use to the following code to filter the letters;
lettersTemp = InputDataLine.match(/[a-zA-Z]+/g);
And then you can append each line in an array.
If you want to append each word in each line in the array, it seems impossible to realize that.
I am trying to replace a two multiline comments (on a single line) with javascript text in the middle. I am using a build tool, which reads the entire file, and need to replace a specific string (made up of comments) during the build.
Example:
var data = /*testThisDelete:start*/new Date();/*testThisDelete:end*/
Once replaced, should used like this
var data = 4.6.88
Try something like this to get started:
"your file as a string".replace(new RegExp('/\*testThisDelete\:start.*testThisDelete\:end\*/','m'), '"replacement text"');
See this post for a lot of useful additional info: JavaScript replace/regex
Are you looking for:
^.+?(\/\*testThisDelete:start\*\/.+?\/\*testThisDelete:end\*\/)$
With this you should just be able to replace the first matched substring with what you want.