RegExp help needed to match ↩︎ (in a parser modual)

Folks, If you look at the core tiddler $:/core/modules/parsers/wikiparser/rules/horizrule.js
(on a copy of empty.html to be safe)

  • You will see it has a regular expression to detect three hyphens, presumably at the begining of a line, or on its own line
    this.matchRegExp = /-{3,}\r?(?:\n|$)/mg;
  • Then if matched replaces this with <hr>

My Regular expression skills are week at best, so I hope someone can tell me the regular expression to match ↩︎ anywhere in the text

  • Then I will clone this tiddler change this to replace ↩︎ with <br> during the parsing.
  • I will make an editor toolbar in support of this, and share back

The idea is to find a single character ↩︎ replacement for the use of <br> in wikitext which looks ugly in text otherwise devoid of html tags.

Your assistance would be greatly appreciated.

Post script;

  • The same regexp can possibly used for many different single character to html tag parser one could think of.
  • Although different parser code may be needed to wrap text etc… ie identify the end of line, which I think this.matchRegExp = /-{3,}\r?(?:\n|$)/mg; is doing.

I think the answer might be right there in your original regexp. The \r looks for a line return and the \n looks for a linefeed (shame that we’re still bound to 20th century carriage printer technologies). The search is looking for the first line feed followed by a line return OR the end of the text.

/\r?(?:\n|$)/mg;
1 Like

regex101.com is great for this sort of thing.

/↩︎\r?(?:\n|$)/gm

↩︎	matches the characters ↩︎ literally (case sensitive)
\r	matches a carriage return (ASCII 13)
?	matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)

Non-capturing group (?:\n|$)
	1st Alternative \n
	\n 	matches a line-feed (newline) character (ASCII 10)
	2nd Alternative $
	$ 	asserts position at the end of a line
Global pattern flags 
	g modifier: global. All matches (don't return after first match)
	m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
1 Like

Thanks @john.edw_gmail.com and @Mark_S

In this case I do not want to test if it is at the end of the line, because this symbol will emulate the end of the line with a <br> tag.

This is some text↩continued, but will break↩︎
This would not be appended above because of the 2nd ↩︎ in the top row

It would seem I need to simply use /↩︎/;

But perhaps my “script kiddy” effort is coming to an end, it appears not to be working;

You were almost there! Two small edits and it seems to work…

change
exports.types = {block: true};
to
exports.types = {inline: true};

and change
this.matchRegExp = /↩︎/;
to
this.matchRegExp = /↩︎/gm;

Thanks Eric, Yes it works, Here is the working version linebreak.js.json (967 Bytes)

If desired this can be turned off with \rules except linebreak and in Info > Advanced > Parsing > Line Break

I will prepare a package with an Editor Toolbar button and leave it here (in this reply) as well, since others may stumble across this thread.

This following includes an editor toolbar button to insert :leftwards_arrow_with_hook:
linebreak-editor-toolbar-button.json (3.2 KB)

  • As its a package containing Javascript save and reload
  • If you installed the package at the top delete $:/core/modules/parsers/wikiparser/rules/linebreak.js

Thanks also @Mark_S and @john.edw_gmail.com