Marked: Why can't I change regex of code

Created on 12 May 2018  路  14Comments  路  Source: markedjs/marked

In the code, you use /^(`+)\s*([\s\S]*?[^`]?)\s*\1(?!`)/ to check for inline code, but if you change it to /^(\$+)\s*([\s\S]*?[^$]?)\s*\1(?!\$)/ its fail, why? how can this affect the code beside it should look for $ insted of `

question

Most helpful comment

@BenjaminHoegh you will also have to change the text regex to stop at $ instead of `

- text: /^[\s\S]+?(?=[\\<!\[`*]|\b_| {2,}\n|$)/
+ text: /^[\s\S]+?(?=[\\<!\[$*]|\b_| {2,}\n|$)/

All 14 comments

I'm not really sure this is a question for marked; looks more like a general regex question for stackoverflow or a regex matcher. It probably isn't working because the $ needs to be escaped with \; in regular expressions, $ is the termination character for the expression.

@robcresswell I know in the example its already written like that, but I only face the problem with _marked_ I then begin to believe its have something to do with other parts of the code there mess this up, but after hours of reading the code I _haven't_ found any reason why it shouldn't work. It also has problems if you change it to other things like % or BB and that's why I choose to write a post here instead of StackOverflow.

What fails? the tests?

@UziTech
udklip

To be precise I do use $$ and not just $ but just needed an example of what's is happening, so in the example it's just a single $

@styfle what information do you need?

@BenjaminHoegh This does not appear to be a problem with marked.

I appreciate your enthusiasm and desire to dig through the source code, but this question would be more appropriate for stackoverflow.com than on this issue tracker.

Maybe in the future we will provide a more extensible API, but that is not available at this time, and your question is about Regex, not marked.


That being said, the answer to your problem can be found here which discusses Special Characters and the need to escape them.

var codeRegex = /^(`+)\s*([\s\S]*?[^`]?)\s*\1(?!`)/;
codeRegex.test('`code`'); // true
codeRegex.test('not code'); // false

var dollarRegex = /^(\$+)\s*([\s\S]*?[^\$]?)\s*\1(?!\$)/;
dollarRegex.test('$dollar$'); // true
dollarRegex.test('no dollars'); // false

Basically, the backslash can be used to escape special characters.

@styfle sure about that? because this is the result I get

// Using:
// code: /^(\$+)\s*([\s\S]*?[^\$]?)\s*\1(?!\$)/,
document.getElementById('content').innerHTML = marked('inline $code$ is awesome'); // Dosn't work

// Using:
// code: /^(`+)\s*([\s\S]*?[^`]?)\s*\1(?!`)/,
document.getElementById('content').innerHTML = marked('inline `code` is awesome'); // Work

@BenjaminHoegh Yes I鈥檓 sure. I wrote the code snippet above to prove it.

Your example is not starting with a dollar sign so it doesn鈥檛 match.

If you want to remove the part that only matches the beginning of a string, remove the first up-carret in the Regex.

Read this for more info: https://www.regular-expressions.info/anchors.html

@BenjaminHoegh you will also have to change the text regex to stop at $ instead of `

- text: /^[\s\S]+?(?=[\\<!\[`*]|\b_| {2,}\n|$)/
+ text: /^[\s\S]+?(?=[\\<!\[$*]|\b_| {2,}\n|$)/

@UziTech is that for block code or inline code?


@styfleI know, but the code there are used in the library to find code inside a paragraph use /^(`+)\s*([\s\S]*?[^`]?)\s*\1(?!`)/ how this can work is a mystery for me as the ^ sign should not work when the code is inside some text, but it does. If you don't believe that the code react in a weird way and not following the normal rules of regex then try to do it in the code yourself by changing the check for ` to $

@BenjaminHoegh inline

I was answering from my phone before so now that I'm back to a computer, I can confirm that changing those two regex works as you expect.

regex-diff

@BenjaminHoegh Did that solve your problem?

@styfle well... kinda works, except that its cut the text before the code... Think I must have messed something up in the process, think I start over and make it in the new version there are just released

Anyways thanks for the help 鉂わ笍 @styfle @UziTech

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FireflyAndStars picture FireflyAndStars  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  4Comments

gclove picture gclove  路  4Comments

bennycode picture bennycode  路  4Comments

james4388 picture james4388  路  3Comments