Pretty simple question, how would I go about cutting the code block identifier out of a string?
These strings should all have the same result:
const str1 = 'Just some example text' const str2 = '``` Just some example text```' const str3 = '```js Just some example text```' const str4 = '```java Just some example text```' const str5 = '```ts Just some example text' const result = 'Just some example text'
There were a few answers floating around, but none of them seemed to deal with language identifiers or relied heavily on newlines.
Advertisement
Answer
The proper answer is to use a Markdown parser.
But we can also bung something together with a regex.
(?:(```)([a-zA-Z]+)?s+)?(.+)1
Pulling it apart…
(?: (```) # start of code, $1 ([a-zA-Z]+)? # code tag, maybe, $2 s+ # always a space )? # that's all optional (.+) # the content 1 # ends with ``` or blank
$1 is the code indicator, $2 is the tag, $3 is the content