Divide the table row and store each cell using regular expressions

Here is the original source text:

gi0/1 1G-Fiber -- -- -- -- Down -- -- Access
gi0/2 1G-Fiber -- -- -- -- Down -- -- 
gi0/3 1G-Fiber -- -- -- -- Down -- -- 
gi0/4 1G-Fiber -- -- -- -- Down -- -- 
gi0/5 1G-Fiber -- -- -- -- Down -- -- 

gi0/0/1 1G-Fiber -- -- -- -- Down -- -- Access
gi0/0/2 1G-Fiber -- -- -- -- Down -- -- 
gi0/0/3 1G-Fiber -- -- -- -- Down -- -- 
gi0/0/4 1G-Fiber -- -- -- -- Down -- -- 
gi0/0/5 1G-Fiber -- -- -- -- Down -- -- 

The code snippet I am working on is as follows:

var re = /(([\w]{2})[\/\d]{1,})\s+([\w\-\.\s]+){1,}[\r\n]/ig;
while ( (MyArray = re.exec(text)) != null ){

// Here I intend to obtain an array with the following values (MyArray)

[0] [matched string]
[1] gi0/1 (gi0/0/1)
[2] gi
[3] 1G-Fiber
[4] -- 
[5] --
[6] --
[7] --
[8] Down
[9] --
[10] --
[11] Access (only for the 1st and 6th row)

// However, the current output in (MyArray) is not correct

[0] [matched string]
[1] gi0/1 (gi0/0/1)
[2] gi
[3] "s" OR "empty"
}

You can view an online example at

I would appreciate any assistance with this. Thank you!

Answer №1

    <script type="text/javascript">
        var subject = "gi0/1 1G-Fiber -- -- -- -- Down -- -- Access";
        //var subject = "gi0/1 1G-Fiber -- -- -- -- Down -- --";
        var regex = /([^\s]*)\s([^\s]*)\s([^\s]*)\s([^\s]*)\s([^\s]*)\s([^\s]*)\s([^\s]*)\s([^\s]*)\s([^\s]*)\s*([^\s]*)/;
        var result = regex.exec(subject);
        document.write("<ul>");
        for (var i = 1; i < result.length; i++) {
            document.write("<li>'<i>" + result[i] + "<\/i>'<\/li>");
        }
        document.write("<\/ul>");
    </script>

If the original string does not contain the word "Access" at the end, this regular expression will still produce an array with a blank final element. Another approach could involve using the String.search method and looping through each character of the source string.

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

Guide to Saving a List in Local Storage

I have successfully created a CRUD page where users can input text and it gets added to a list. Now, I am trying to save that list in localStorage but encountering an issue where the console log is showing an empty object. Any assistance on this matter wou ...

Transmit data from the Windows Communication Foundation to JavaScript

Looking to invoke the WCF service through JavaScript, utilizing AJAX? Check out this resource: Calling WCF Services using jQuery Here's my query: Is there a method to retain some JavaScript-related data after making a request, and then transmit inf ...

Creating an executable JAR for your Next.js application: A step-by-step guide

Is there a way to ensure that my Next.js file can run seamlessly on any computer without the need for reinstallation of all modules? In my folder, nextjs-node, I have the following subfolders: components lib public node_modules page style package.json I ...

Node.js application for changing attributes in an HTML string

Within my node.js backend, there exists an object with a specific property named content, which stores an HTML string. The content within includes an img tag that currently has a src attribute containing a base64 string. I am seeking to modify this src att ...

Troubles encountered when cascading val(), text(), and data()

Here is my JavaScript/jQuery code: $select.append($("<option />") .val(this.id) .text(this.text) .data('name', this.name) .data('isstorage', this.isstorage)); Although it successfully assigns values to t ...

Adjust the text size of a label in material-ui

Hey everyone, I'm struggling with something here. I need to adjust the font size of the label that goes along with my textfield. So far, I've only been able to change the font size of the input itself and not the label. Here's the code for ...

What is the best way to incorporate a JavaScript file into my ejs file using node.js?

As I work on creating a website for my school project, I encountered a challenge involving including CSS files in node.js. Thankfully, after researching on stackoverflow, I was able to find a solution. Now, I am facing another issue - how to include a java ...

Determine the name of the time zone using JavaScript

Currently, I am developing a React application and facing an issue where the timezone is detected as 'Etc/GMT-1' instead of the desired format 'Africa/Bangui'. This problem seems to be specific to my machine as it persists even when usi ...

Display one of two divs after clicking a button depending on the input from a form field

My apologies for my limited knowledge in JavaScript and jQuery. I'm attempting to show one of two divs based on input from a form. Specifically, there's a button next to a form field that asks users to enter their zip code to check if they are wi ...

Display all items on page load using ng-repeat checkboxes in AngularJS filter

I have encountered a problem with filtering checkboxes in my code. I want all products to load on the page initially, and then when I check multiple checkboxes within technologyArray and/or technologyArray2, the products that match the checkbox name should ...

Error message: "Angular dependencies and provider not accessible"

I recently came across a file upload script on Github that I decided to implement. <script type="text/javascript" src="{% static 'bower_components/angular-file-upload/angular-file-upload.min.js' %}"></script> Furthermore, I have cre ...

Utilizing Jquery or Javascript to Establish a Fresh Perspective for CylinderGeometry with Three.js

I'm attempting to change the dimensions of a cylinder created using examples from Three.js at runtime, but my code doesn't seem to be working. Here is the code snippet I am using: HTML <script src="http://www.html5canvastutorials.com/librari ...

Multiple requests were made by Ajax

I am facing an issue with my ajax code where it is being called multiple times. Below is my php code: (While loop extracts results from a database. For brevity, I have included just the modal and dropdown menu sections.) $rezPet = mysqli_query($kon, "SEL ...

Using AngularJS, you can easily preselect an option in a select menu that is populated using ng-options

Exploring how to use AngularJS ng-option directive to populate a select menu and set a default option. The JSON data retrieved by AngularJS: {"AF":"Afghanistan","AX":"\u00c5land Islands","AL":"Albania","DZ":"Algeria","AS":"American Samoa","AD"} The ...

Design a unique input component tailored specifically for react-day-picker

While working on a custom Material UI component for DayPickerInput, I encountered an issue where the onDayChange event triggers an error. handleToDateChange = (selectedDay, modifiers, dayPickerInput) => { const val = dayPickerInput.getInput().value ...

In the virtual playground of Plaid's Sandbox, how can I replicate a fresh transaction and detect it using the Webhook feature?

Is there a way to trigger a simulated transaction within the webhook instead of just a DEFAULT_UPDATE event? I'm trying to find a way to simulate an actual transaction so that I can test my webhook integration. I've searched through the sandbox ...

How do I go about showing every character on a separate line using a for loop?

var input = prompt("What is Lance attempting to convey?"); //user enters any text for (var i = 0; i <= input.length; i++) { var output = input.charAt(i); if (output == "e" || output == "o" || output == "a" || output == "u") { outp ...

Error: The object does not contain the specified method

After deploying a web app to a remote host, I encountered an unusual error. Uncaught TypeError: Object #<error> has no method 'endsWith' Key Information: The application functions perfectly on my local machine as the host. The error ari ...

Troubleshooting compatibility issues between Sailsjs Services and TypeScript in Vscode

Having an issue with TypeScript in a Sails.js application. I am utilizing TypeScript to write my controller and attempting to use Sails.js services within the controllers. However, I encounter a syntax error in VSCODE. Below is the code snippet from MyCo ...

"Positioning a div around a Bootstrap form-inline: A step-by-step guide

When using Bootstrap 3 "form-inline" within a <div>, I noticed that the div seems to be nested within the form-inline. This is how my code looks: HTML <div class="wrapper"> <div class="form-inline"> <div ...