Transform words into an array

Help needed.

The given input text is

"['(', /[1-9]/, /\d/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/, /\d/]"

I am trying to convert it into an array

['(', /[1-9]/, /\d/, /\d/, /\d/, ')' ,' ' , /\d/, /\d/, /\d/, /\d/, /\d/, /\d/, '-' ,/\d/, /\d/, /\d/, /\d/, /\d/, /\d/]

Unfortunately, I am facing issues as it adds quotes.

I have attempted using the split() method and tried the flat() method as well. Is there any other solution?

Answer №1

Handling String Types in Javascript Requires Special Instructions

In order for Javascript to effectively deal with the two different types of strings, specific actions need to be taken.

To properly handle this, instruct Javascript to eliminate ' characters and create a regular expression for instances involving the / character.

The following code snippet is designed to achieve the desired outcome:

const s = "['(', /[1-9]/, /\d/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/, /\d/]"

const out = s.slice(1, s.length - 1).split(", ").map(e => {
  if (e.startsWith("'")) {
    return e.slice(1, e.length - 1)
  }
  if (e.startsWith("/")) {
    return new RegExp(e.slice(1, e.length - 1))
  }
  throw new Error("Unexpected first character in element " + e)

})


console.log(out)

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

The significance of JavaScript Namespace objects and the order in which scripts are included

I'm encountering an issue with JavaScript namespace conflicts. To organize my code, I've split my JavaScript objects into different files. Each file begins with a namespace declaration: var MySystem = MySystem || {}; However, when I include a ...

Accessing the 'comment' property within the .then() function is not possible if it is undefined

Why does obj[i] become undefined inside the .then() function? obj = [{'id': 1, 'name': 'john', 'age': '22', 'group': 'grA'}, {'id': 2, 'name': 'mike', &apo ...

Is it possible to have an optional final element in an array?

Is there a more graceful way to declare a Typescript array type where the last element is optional? const example = (arr: [string, string, any | undefined]) => console.log(arr) example(['foo', 'bar']) When I call the example(...) f ...

What is the process for creating a register command using discord.js and MongoDB Atlas?

How can I save my Discord member data using a register command? Please provide assistance! bot.js client.on("message", msg => { if (msg.content === "!register, ign:<input from member>, level:<input from member>"){ ...

Adding QML code into a Jade file

Currently working on developing a straightforward video streaming application using Node.js and integrating the WebChimera plugin. The player configuration is done in QML with Chimera, and I am facing numerous errors during the compilation process in Jade. ...

Tips and Tricks for Managing an Extensive Array of AJAX Requests (Exceeding 1000)

My application is retrieving a User's Google Contacts from the Google Contacts API on the front end, resulting in a variable number of JSON objects, usually ranging between 1 to 2000. Upon receiving these objects, the app goes through each one, reform ...

Error message "Jquery smooth scrolling issue: Unable to retrieve property 'top' as it is not defined"

I'm currently working on a script for a back to top button that will be placed in the footer of my website. However, I'm running into an issue where I'm getting an error message saying "Cannot read property 'top' of undefined". Any ...

What are some ways to connect my CSS styles without encountering a MIME error?

Working on a Nodejs - Express - Vanillajs project, I encountered an issue when testing my routes. The index page failed to load CSS, displaying the following error message: Refused to apply style from 'http://localhost:3000/public/css/bootstrap.min. ...

Implementing pagination in Webgrid using AJAX post method

I've developed this JavaScript code: function PartialViewLoad() { $.ajaxSetup({ cache: false }); $.ajax({ url: "/ControllerAlpha/MethodBeta", type: "GET", dataType: "html", data: { s ...

What is the best way to send the value of a Select component from a child to a parent component in

Users have the ability to select a category, triggering the appearance of another dropdown menu based on their selection. I have created a separate component in a different file (.js) to handle this second dropdown. While I can see the data, I am wondering ...

Utilize PHP's file_get_contents to trigger the Google Analytics tracking pixel

For certain goals I have set up in Google Analytics, I am unable to use the JavaScript tracking. Instead, I am interested in achieving the same result by making a call with PHP. One solution that seems straightforward to me is to invoke the URL of the tra ...

Starting a blank list and performing various adjustments

Here is my current coding challenge: I have a list containing strings as elements. I want to perform more than two operations on this list. Create an empty list inside the class. Manipulate each element in the original list and then add it to the empty l ...

The radio button element could not be located using the attribute "checked="Checked""

While working with Geb, I encountered an issue using the code div.find("input","checked":contains("checked")). This is the snippet of code I attempted to use in order to locate the checked radio button on a webpage. However, I received an error when using ...

Locate the element within the specified parameters using [protractor]

Here's my query: element.all(by.repeater('user in users')).then(function(rows) { // looking to locate an element in rows using CSS selector, for example } UPDATE : I want to clarify that I am trying to find an element using rows[rows.len ...

What is the best method for properly inserting images into a Vue Carousel 3D slider?

I'm currently exploring the Vue Carousel 3D documentation and I am having trouble displaying a single image on each slide. Let me elaborate: I aim to create a slider similar to the one shown in the Controls Customized example in the documentation. Th ...

The request cannot be fulfilled as Forefront TMG is requesting authorization. Access to the Web Proxy filter has been denied

I recently created a website that utilizes Ajax POST with jQuery to communicate with a PHP server. Everything works smoothly when I access the website from my home browser, but some of the Ajax requests fail when I try to access it using my corporate netw ...

Disabling past dates in a React JS application with a React date picker

Can someone help me figure out how to prevent selecting past times in React JS? Here is the code snippet: import DatePicker from "react-datepicker"; import setHours from "date-fns/setHours"; import setMinutes from "date-fns/setMi ...

Steps for rotating around the x-axis followed by the y-axis with quaternions

Is there a way to rotate an object around its x-axis by 90 degrees and then around its y-axis by another 90 degrees in three.js? I experimented with the following code: mesh.rotation.x = Math.PI * 0.5; mesh.rotation.y = Math.PI * 0.5; However, the objec ...

What steps should I take to incorporate Bootstrap's JavaScript into Vue (Gridsome)?

Check out my website: The site is built with Gridsome, a static site generator for Vue. If you navigate to the mobile version and try to open the Bootstrap Hamburger menu, it doesn't work as expected. I've followed the instructions in Gridsome ...

Is a switch statement supported by jade's syntax?

When attempting to implement a switch statement in Jade served by Express, I encountered an error message stating "unexpected identifier". - switch(myvar) - case: "0" span First Case break - case: "2" span Second Case ...