What is the best way to exclude a certain string when splitting in JavaScript?

I am developing an application that breaks down markdown content into a string of '---' and presents it as a slide. The challenge I am facing is that when there is table markdown included, like in the example below, it gets split along with the '---' causing an error. How can I modify my code to only split '---' as a string and exclude the table markdown?

# Title

---

|hello|world|
|---|---|
|asd|zxc|

Answer №1

If you decide to utilize a regex for the splitting process, it allows you to customize how the split occurs.

For example, if the --- that you wish to split are consistently on their own lines, then using /^---$/gm as the delimiter is an effective approach.

The m flag indicates to the regex that multiline mode is activated - meaning that ^ and $ will match the start and end of each line, rather than the entire string.

const string = `# Title

---

|hello|world|
|---|---|
|asd|zxc|`;

console.log(string.split(/^---$/gm))

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

working with the express locals function

I've been trying to come up with a function that can access local variables, such as this one: // Retrieve user data by ID res.locals.findUser = function(user_id) { models.user.findOne({ '_id': user_id }, function(err, user) ...

Stop unauthorized HTTP requests by limiting a web application to only communicate with specified domains

I have come up with an innovative security feature idea for browsers/web clients, but I have not been able to find any information or discussions about it. Therefore, I am seeking an answer to determine the feasibility and necessity of such a feature. Que ...

Using the "this" keyword within a debounce function will result in an undefined value

It seems that the this object becomes undefined when using a debounce function. Despite trying to bind it, the issue persists and it's difficult to comprehend what is going wrong here... For instance, in this context this works fine and returns: Vu ...

I am encountering the error 'user.matchPassword is not a function' while making a call to my API using bcryptjs in my Node.js and Express application. Can someone help me understand why

const checkUserAuth = asyncHandler( async (req,res)=>{ const { email , password } = req.body; const foundUser = User.findOne({email}); if(foundUser && (await foundUser.verifyPassword(password))){ generate ...

Using Vue.js to summon a method from within a data property in a component

Can component methods be assigned to data variables using the example code below? It seems it is not working correctly. What could be the right way to do this? <li v-for="item in items" @click="item.action"> {{ item.title }} ...

Validation of OpenAPI requests on the client-side using React libraries

Is there a way to validate a request against a specific openAPI spec on the client side in a browser environment? I've spent countless hours searching and trying various openapi-tools, but all seem to be geared towards nodejs usage and not suitable f ...

Do not use npm to install underscore libraries

How can I resolve the error I encountered while attempting to install packages using npm? Here is my packages file: "dependencies": { "express": "~3.3.6", "socket.io": "0.9.16", "jade": "~0.35.0", "less-middleware": "~0.1.12", "redis ...

Every time Jquery tries to retrieve cookies, it consistently returns as undefined

Having trouble accessing Application cookies using jquery in my Asp.Net MVC App. Check out this Screenshot of Cookie and its Value. I've been trying to access the Cookie with $.cookie('ASP.NET_SessionId'); but it keeps returning "undefined" ...

Retrieving data from a simulated angular service and making calls accordingly

I'm struggling with writing tests for a controller method that relies on an Angular service call and needs to test the code inside the .then() function afterwards. Here's the basic concept: // Controller $scope.myMethod = function() { meth ...

Using ASP.NET MVC 6 Web API along with Angular, a resource can be posted as an object property in the

I am facing a challenge in sending a complex object from Angular to my web API. Here is how the object looks: { Name: "test", Tax: 23, Addresses: [ { country: "ro", city: "bucharest" }, { country: "fr", ...

Is it possible to run both the frontend and backend on the same port when using vanilla JavaScript for the frontend and Node.js for the backend?

Is it possible to run frontend and backend on the same port in Rest APIs if using vanilla JS for the frontend and Node.js for the backend? I've come across information on how to do this with React, but nothing specific to vanilla JS. Any insights on t ...

"Using Sequelize's Op.and and Op.like operators led to an unexpected outcome of producing an empty

I am working on developing a search endpoint using express and sequelize. I noticed an issue where using Op.and in my 'where' object results in an empty object: const where = { [Op.and]: req.query.q.split(" ").map((q) => { ...

Reloading data in Angular using jQuery DataTables

After successfully implementing the jQuery datatables library, I encountered an issue where new data retrieved from my API was not displaying inside the datatable as expected. Instead, it was being shown below the table using ng-repeat. It seems that the d ...

Tips for adjusting the horizontal position of a grid item within a map() loop

I am trying to align the text in my Timeline component from Material Ui always towards the center of the timeline. The TimelineContent contains Paper, Typography (for title and description), and an image. Currently, I have multiple TimelineContent element ...

Exploring the functionality of the JavaScript switch statement across various condition scenarios

switch (true) { case (angle<20): console.log("case1") break; case (angle<70): console.log("case2") break; case (angle< ...

Are you familiar with manipulating Arrays or Objects in jQuery?

In JavaScript, we can create objects with keys as strings for similar functionality to an associate array. Check out this example here. For example: ".home-title":[ ["font-size","12px"], ["line-height","16px"], ], However, if you need a ...

Initiate a file download following the redirection of a user to a new page in NodeJS/Express

Overview I am currently developing a NodeJS project using Express. In the application, there are buttons visible to the public that trigger file downloads. These buttons are linked to protected routes which will redirect users to a login page if they are ...

Setting a Fixed Default Value in an Angular Dropdown Menu

Within my code, there is a specific column where users have the ability to insert an element and choose its priority type while doing so. I am currently attempting to assign a default value to the dropdown selection (row.PriorityType.Id ==1). Although I at ...

Anticipating the asynchronous completion of all nested recursive functions

I have a recursive function that makes asynchronous calls, using the results of those calls as arguments for subsequent recursive calls. I am trying to find a way to wait for all recursion levels to complete simultaneously, without any delays at each leve ...

Creating dual graphs simultaneously in Rickshaw

Can two graphs with different Y axes be plotted together? I have data on page views organized in a stacked area chart by referrer, and I would like to overlay a line graph depicting the number of actions over time. Although I already have both graphs ind ...