Breaking down a statement into individual elements within an array, incorporating keys alongside the elements within the array

let condition = "[AccNum]==true&&[AccNum]==[ARID]&&[AccNum]==aaaa || [ARID]!=true&&[DOB]>[ARID] || [DOB]>bbb&&[DOS]>=[ARID]&&[DOS]<[Gender]&&[66642]<=cccc&&[66642] In (ddd,fff,ggg) || [FirstName] NotIn (hhh,jjj,kkk)&&[FirstName] StartsWith lll || [Gender] EndsWith mmm";

I am looking to divide this into an array like below,

let array = [ "[AccNum]==true","&&", "[AccNum]==[ARID]", "&&", "[AccNum]==aaaa", "||", "[ARID]!=true", "&&", "[DOB]>[ARID]", "||", "[DOB]>bbb", "&&", "[DOS]>=[ARID]", "&&", "[DOS]<[Gender]", "&&", "[66642]<=cccc", "&&", "[66642] In (ddd,fff,ggg)", "||", "[FirstName] NotIn (hhh,jjj,kkk)", "&&", "[FirstName] StartsWith lll", "||", "[Gender] EndsWith mmm" ]

The separators in the expression are "&&" and "||" and I want them included as keys in the array too.

Answer №1

let sentence = "[AccNum]==true&&[AccNum]==[ARID]&&[AccNum]==aaaa || [ARID]!=true&&[DOB]>[ARID] || [DOB]>bbb&&[DOS]>=[ARID]&&[DOS]<[Gender]&&[66642]<=cccc&&[66642] In (ddd,fff,ggg) || [FirstName] NotIn (hhh,jjj,kkk)&&[FirstName] StartsWith lll || [Gender] EndsWith mmm";

let parsedSentence = sentence
                .split(/(&&|\|\|)/)
                .map(item => item.trim());

console.log(parsedSentence)

Using RegEx to split the sentence and map function for trimming each item.

Answer №2

Here is a suggestion:

Let's try the following code snippet:

var statement = "[AccNum]==true&&[AccNum]==[ARID]&&[AccNum]==aaaa || [ARID]!=true&&[DOB]>[ARID] || [DOB]>bbb&&[DOS]>=[ARID]&&[DOS]<[Gender]&&[66642]<=cccc&&[66642] In (ddd,fff,ggg) || [FirstName] NotIn (hhh,jjj,kkk)&&[FirstName] StartsWith lll || [Gender] EndsWith mmm";
var splitData = statement.split(/(&&|\|\|)/g);

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

I'm curious as to why IPC messages from one menu item in Electron can successfully reach my window, but when sent from a different menu item, they do not seem to

I am working on a straightforward application that requires running a background process to fetch some data. I want to display a loading indicator while the data is being retrieved, but I am encountering difficulties implementing this feature. My approach ...

Is it possible to conceal or disregard text elements from the web browser search function by using CTRL+F?

I have a complex web application interface with a dedicated area for content display. Is there a way to ensure that when users utilize the browser's text search function (CTRL+F), the UI elements are ignored and only the actual content is searched? ...

What is the best way to check for incorrect or missing values when comparing two Arrays?

Can you help me with comparing two arrays, $original and $duplicate? Here is the content of my original array: print_r($original); Array ( [0] => cat423 [1] => dog456 [2] => horse872 [3] => duck082 ) And this is my duplicate array: pr ...

Looking for a personalized function to iterate through every displayed row and reduce the number of API requests made

UPDATE: Seeking guidance on consolidating functions and implementing a trigger. Within my spreadsheet, I manage rows 4-100 that represent customer service calls filtered from a "ServiceData" worksheet by selecting either a "Service Month" or "Service Day" ...

Unable to fetch the identification number from the database

I'm encountering an issue with retrieving the ID from my database: https://i.sstatic.net/oSAi8.jpg Here is a snapshot of my database: https://i.sstatic.net/j5PpZ.jpg Below is my event controller class: <?php namespace App\Http\Contro ...

Ensure that the submit button triggers the display of results with each click

My project involves two navigation bars, each with its own table displayed upon page load. Additionally, there is a search bar used to display search results in another table. The issue I'm encountering is that when I click the submit button once, th ...

I am looking to modify the appearance of specific sections of a searched word in React.js, such as making them bold or lighter. Can anyone

After coming across several similar questions, I realized none quite matched my issue. Specifically, I am attempting to style only the part of the search result that relates to the entered query. For instance, if the query is "Thi", I want the result to di ...

Creating a customized store locator using JSON data in WordPress for Google Maps API

I'm currently seeking a customized solution to implement a store locator using the Google Maps API within WordPress. Although there are numerous WordPress plugins available, I prefer a more tailored approach. Here are the specific requirements: ...

Angular.js: Applying a style to elements beyond the current scope based on the selected route

I'm looking to dynamically add classes outside of the current scope. Specifically, I need to add classes to the header, main, and footer elements as shown below: <header class="{{pageClass}}"></header> <main class="{{pageClass}}" ng-vi ...

Unable to display image on React page using relative file path from JSON data

Greetings, this is my initial post so please forgive me if I have missed any important information. I'm currently in the process of creating a webpage using react. My goal is to display content on the page by iterating over a relatively straightforwa ...

Error message: AngularJS Jasmine spyOn: number has no functionality

I am encountering difficulties while attempting to execute a test that utilizes a mockup for a service call (retrieving location data from a web SQL database). Below is the controller code: .controller('LocationDetailCtrl', function ($scope, $s ...

Sending various array data via AngularJS

Welcome to the HTML page view where users can input data and send it to an API. https://i.sstatic.net/uJTYc.png Within the form, three parameters need to be passed in a single array: day, fromtime, and to-time. How can these three parameters be passed in ...

Implement rounded corners on D3js Donut Chart

In my AngularJS application, I have successfully implemented a donut chart as shown below: https://i.stack.imgur.com/3GVwN.png However, the design mockup indicates that we would like to achieve this look, with a border-radius property applied to the gree ...

What is the best way to set the `value` attribute and `v-model` attribute for a custom Vue component?

I am seeking to develop a unique Vue component that functions as a "radio div" - essentially, a div with the ability to act like a radio button where only one among multiple can be selected at a time. The div will also have a slot for including any desired ...

Adding a unique element to other elements in jQuery/Javascript can be achieved even when the existing elements only have classes assigned to them

One interesting feature of a Wordpress theme is the ability for site administrators to create forms with descriptions for each field. These descriptions share a common class, 'description,' and look like this: <div class="field-head"><l ...

Using single page anchor tags in Next.js to toggle content visibility

The Issue Currently working on a project using Next.js and facing a challenge: needing to hide or replace content based on the selected category without reloading the page or navigating to another route. Furthermore, ensuring that when the page is reloade ...

What is the best method for importing a single module within a monorepo project using JavaScript and NPM?

I've organized my codebase into a monorepo with the following structure: ➜ yw git:(master) tree . ├── package.json ├── packages │ ├── common │ │ ├── package.json │ │ ├── src │ │ │ ├─ ...

Jade fails to show image in route with parameter

Currently, I am utilizing express 4 and have images saved in the uploads directory. This is a snippet of my code: app.use(express.static(__dirname + '/uploads')); //This part works app.route('/') .get(function (req, res) { ...

What is the best way to transfer a variable from an @Input property to a service within an Angular2 component?

I'm tackling what seems like a simple task, but I'm struggling to figure it out. My issue is this: How can I successfully pass a variable from @Input to a service in an Angular2 component? (Code has been simplified) This is my current component ...

"Exploring the process of assigning input data to a different variable within a Vue component

Reviewing the code snippet I currently have: <template> <div> <input v-model.number="money"> <p>{{ money }}</p> </div> </template> <script> name: 'MyComponent', data () { ...