Extract the last word from a string, filter out any special characters, and store it in an array

Here is the content of the xmlhttp.responseText:

var Text = "FS2Crew A320 Checklist_1""FS2Crew Flight Crew A320 Main Ops Manual_1""FS2Crew Flight Crew A320 Main Ops Manual_10""FS2Crew Flight Crew A320 Main Ops Manual_11""FS2Crew Flight Crew A320 Main Ops Manual_12""FS2Crew Flight Crew A320 Main Ops Manual_13""FS2Crew Flight Crew A320 Main Ops Manual_14""FS2Crew Flight Crew A320 Main Ops Manual_2""FS2Crew Flight Crew A320 Main Ops Manual_3""FS2Crew Flight Crew A320 Main Ops Manual_4""FS2Crew Flight Crew A320 Main Ops Manual_5""FS2Crew Flight Crew A320 Main Ops Manual_6""FS2Crew Flight Crew A320 Main Ops Manual_7""FS2Crew Flight Crew A320 Main Ops Manual_8""FS2Crew Flight Crew A320 Main Ops Manual_9""FS2Crew Pushback Express Main Ops Manual_1""FS2Crew Pushback Express Main Ops Manual_2""FS2Crew Pushback Express Main Ops Manual_3""FS2Crew Pushback Express Main Ops Manual_4""FS2Crew Pushback Express Main Ops Manual_5""FS2Crew Pushback Express Main Ops Manual_6""FS2Crew Pushback Express Main Ops Manual_7""FS2Crew Pushback Express Main Ops Manual_8""VFR-Ffdfdfdlight-Planner-Dax_1""VFR-Ffdfdfdlight-Planner-Dax_2"

To achieve this, I want to extract specific strings from the content and form an array as follows:

result = [FS2Crew A320 Checklist~1, FS2Crew Flight Crew A320 Main Ops Manual~14, FS2Crew Flight Crew A320 Main Ops Manual~8, VFR-Ffdfdfdlight-Planner-Dax~2]

The desired format for these strings in the array is to remove the character (") and separate the file name from the occurrence count with (~).

Please overlook any errors in my English.

Answer №1

I successfully eliminated the characters and constructed an array.

Below is the function:

    function retrieveFiles(){
        var location = '/images';
        var xmlhttp = new XMLHttpRequest();
        src='files.php'
        xmlhttp.onreadystatechange = function() {

            if (xmlhttp.readyState == XMLHttpRequest.DONE) {   // XMLHttpRequest.DONE == 4
                if (xmlhttp.status == 200) {
            
                    var list = xmlhttp.responseText;
                    var arrStr = list.split(/[""][""]/);
                    var arrayLength = arrStr.length;
                    
                    arrStr.forEach(function (item) {
                        filename = item.replace('"', '')
                        console.log(filename);
                    });
                }
                else if (xmlhttp.status == 400) {
                    alert('Error 400 occurred');
                }
                else {
                    alert('A different status other than 200 was returned');
                }
            }
        };
        xmlhttp.open("GET", "filelist.txt", true);
        xmlhttp.send();
    }   

Here is the outcome:

result: [FS2Crew A320 Checklist_1, FS2Crew Flight Crew A320 Main Ops Manual_1, FS2Crew Flight Crew A320 Main Ops Manual_10, FS2Crew Flight Crew A320 Main Ops Manual_11, FS2Crew Flight Crew A320 Main Ops Manual_12, FS2Crew Flight Crew A320 Main Ops Manual_13, FS2Crew Flight Crew A320 Main Ops Manual_14, FS2Crew Flight Crew A320 Main Ops Manual_2, FS2Crew Flight Crew A320 Main Ops Manual_3, FS2Crew Flight Crew A320 Main Ops Manual_4, FS2Crew Flight Crew A320 Main Ops Manual_5, FS2Crew Flight Crew A320 Main Ops Manual_6, FS2Crew Flight Crew A320 Main Ops Manual_7, FS2Crew Flight Crew A320 Main Ops Manual_8, FS2Crew Flight Crew A320 Main Ops Manual_9, FS2Crew Pushback Express Main Ops Manual_1, FS2Crew Pushback Express Main Ops Manual_2, FS2Crew Pushback Express Main Ops Manual_3, FS2Crew Pushback Express Main Ops Manual_4, FS2Crew Pushback Express Main Ops Manual_5, FS2Crew Pushback Express Main Ops Manual_6, FS2Crew Pushback Express Main Ops Manual_7, FS2Crew Pushback Express Main Ops Manual_8, VFR-Ffdfdfdlight-Planner-Dax_1, VFR-Ffdfdfdlight-Planner-Dax_2]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust variable values in real time based on user input

In my C# code, I have initialized an array with values data1, data2, and data3 that are manually assigned to 'row' as shown below. string[] row = new string[] { type, data1, data2, data3, shares }; Instead of manual coding, I want to dynamicall ...

Tips for waiting for a Promise to resolve before returning a new Promise

In the process of developing a web application tailored for crafting retail signage, I have integrated Firestore to manage pertinent information about these signs. One specific functionality I am working on is enabling users to effortlessly remove all exis ...

problems encountered when trying to deploy a backend api on the Render platform

Encountered this error: May 14, 04:27:30 PM - Error [email protected]: The "node" engine is not compatible with this module. Expected version ">=14.20.1". Received version "14.17.0". May 14, 04:27:30 PM - Incompatible module detected. Verified my ...

Utilizing AJAX to amplify the worth of plupload's echo capabilities

Greetings! Here is the JavaScript code I am using for plupload var uploader = new plupload.Uploader({ runtimes : 'html5,flash,silverlight,html4', browse_button : 'pickfiles', // you can pass in id... container: document.getElementById( ...

Confirm that the input into the text box consists of three-digit numbers separated by commas

Customers keep entering 5 digit zip codes instead of 3 digit area codes in the telephone area code textbox on my registration form. I need a jQuery or JavaScript function to validate that the entry is in ###,###,###,### format without any limit. Any sugge ...

Having trouble with using findByIdAndUpdate and push in MongoDB?

As someone who is new to Mongodb, I have been using the findByIdAndUpdate function to update a document in my project. However, I noticed that it returns the old document instead of the updated one. Below is the code snippet of my function: exports.crea ...

When making an AJAX request and sending a JSON object, the server is returning an undefined

My current setup involves using an XMLHttpRequest in the following manner: xml.send(JSON.stringify({ingredients: this.state.ingredients})); This is used to transmit an object (this.state.ingredients) to the server. The payload appears correct when checke ...

Looking for insights on reading and presenting XML files from a URL? Learn how to achieve this effortlessly with the power

Currently, I'm in the process of developing an innovative Android application using PhoneGap. My main objective is to dynamically present the data stored within a customized XML file hosted on my website that follows the .php format. Do you happen to ...

Run code after all images have been rendered in vuejs

Is there a way to execute code after all images have loaded, specifically needing to set the scroll in a specific position? Using nextTick() processes the code before the images are loaded. The mounted and created methods can't be used since the code ...

encountering an issue with data[i].order not being recognized as a function

I have a task to complete, which involves calling JSON objects based on a search and displaying the results in a bootstrap table. I have been trying to solve this in different ways, but as I am new to JS, I haven't been successful. I have been writing ...

Turn off error notifications from eslint parsing

Within my code, there is a conditional export that looks like this: if (process.env.NODE_ENV === 'testing') export myFunc; While in es6, this type of statement is typically not allowed due to the requirement for imports and exports to be top- ...

Having trouble with React testing-library: Why is the file upload empty when testing a PDF file?

While testing file upload with react-testing-library, I encountered an issue where the log indicated that the file was empty (even though it worked in the browser). After researching various docs and bugs, I discovered that since tests run on Node.js, the ...

Determine in C++ whether a given input string matches the array string in a class

I'm currently facing an issue with searching for a book in my array using either the title or ISBN. Despite implementing a search function, it fails to find books that are already in the array. The code seems correct to me, but I can't figure out ...

What is the best way to continuously compare two date variables every minute using Javascript?

In my script, I have two date variables - one representing the current time and the other two minutes later. My goal is to compare both values every minute and trigger a function when the current time is greater than or equal to the latter time. Unfortun ...

Caution: the index of the array is outside the boundaries of the array [-Warray-bounds] within the

static int myarray[2]={-1,234}; module_param_array(myarray,int,&arrayargc,0); MODULE_PARM_DESC(myarray,"Integer Array"); static int __init module_init_2(void) { int i; for(i=0;i< (sizeof myarray/sizeof(int));i++); { printk(KERN_INFO "mya ...

Can express middleware be tailored for each individual handler within the same route path?

I am seeking to create distinct routes under an /api path with varying middleware handlers, each allowing for different authentication methods. My initial approach was to nest these API routes under separate instances of the Router object using Express: c ...

What could be the reason for the inability to install Angular JS 2?

Setting up Angular 2 for experimentation on my VPS has been quite a challenge. The initial steps can be found here. Upon trying the first command: $ npm install -g tsd@^0.6.0 I encountered success. However, the second step led to an error: tsd install ...

Attempting to transfer information from a JSON file to a Netflix-inspired platform

Images I am currently working on integrating data from my json file export const products = [ { name: 'iPhone 11', image: '/assets/images (7).jpeg', time: '1 hour 14mins', age: '+16&apo ...

Summernote information embedded with HTML elements

I just started using the summernote text editor and I'm trying to figure out how to extract the content from the textarea without all the HTML tags. This is what I have in my code: <textarea class="summernote" id="summernote" ng-model="blog.c ...

The TextBox will alter its color following an incorrect submission

I am struggling to create a bootstrap form that will change the color of the borders to red after incorrect submission. The issue I am facing is that the textbox always remains in red. Does anyone have any suggestions for setting the textbox borders to red ...