0

I'm making an app that splits the markdown content like the one below into a string of '---' and displays it as a slide. However, even if there is table markdown as shown below, it is split together and an error occurs. How can I split only '---' as a string, excluding table markdown?

# Title

---

|hello|world|
|---|---|
|asd|zxc|
1
  • You can use ^---$ in regex to exclude pattern with three dashes. Commented Jul 23, 2021 at 2:05

1 Answer 1

2

If you use a regex for the split, then you can adjust how you split.

For instance, if the --- you want to split are always alone on their lines, then splitting off of /^---$/gm works well.

The m is a flag to tell the regex that it's in multiline mode - I.e. ^ and $ match to each individual line, rather than to the full string.

const string = `# Title

---

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.