0

In a string like

Hallo, this is <code>`code`</code> and this `is code again`.

To analyse it, parse it with regex?

In this example the user just typed the far right ` at the very last. The first "code" has obviously already been surrounded by HTML.

I need a regex to get the next code indicated part.

There always be one series, that is valid markdown AND not already surrounded by the corresponding HTML tags.

How to get this specific series (regardless if it's *, **, ___, ` or whatever)?

2 Answers 2

1

So what you want is a regex that only matches the markdown that isn't surrounded by HTML tags right ?

You can use something like this :

/(?:[^<>]|^)(`[^<>].*?`)/

This will only match the text placed inside `` that aren't directly placed next to a < or > character. This way, no matter what the HTML tag is inside the <...>, the `code` won't match.

See this Regex101.com

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

5 Comments

This does not work, if the string starts with an Single Quotation Mark and adds a Whitespace to every piece of code
What do you mean exactly ? That it matches this : "<code> code</code>" ? If so you're right. The regex you provided does the same though.
No i mean if the whole thing starts with a "´" See: regex101.com/r/mhfUFQ/2 . And your regex matches the character befor the "´" too
Sorry, my bad. I don't want to match the 'code' (Used ' here while I actually mean `) inside the <code>..</code> block. I just want to match those, that aren't surrounded by HTML yet (but with the right and left ` included)
I updated my answer. It now matches a `code` that would be placed at the beginning of the sentence. And the capturing group now includes the character "`". The advantage of my solution is that it's more generic. It doesn't matter what the HTML tag is inside the <...>.
0

If you want to match every emphasized string that is not tagged with "code" you can use

(?<!<code>)`[\w ]+`

You can test it on regex101.com

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.