How to replace all lines in multiline string starting with #
str.replace(/^#([^\n]*)\n$/gm, '<h1>$1</h1>')
multiline string
# headline
some text
# new headline
some more text
result string
<h1>headline</h1>
some text
<h1>new headline</h1>
some more text
How to replace all lines in multiline string starting with #
str.replace(/^#([^\n]*)\n$/gm, '<h1>$1</h1>')
multiline string
# headline
some text
# new headline
some more text
result string
<h1>headline</h1>
some text
<h1>new headline</h1>
some more text
Share
Improve this question
edited Nov 18, 2013 at 12:33
clarkk
asked Nov 18, 2013 at 12:28
clarkkclarkk
1
1
- So the goal is to match everything from any start-of-line + hash to the closest end-of-line? Why does that feel trivial to me? – John Dvorak Commented Nov 18, 2013 at 12:31
3 Answers
Reset to default 8try this regexp /^#(.*)$/mg
like this
str.replace(/^#(.*)$/mg,"<h1>$1</h1")
If your line breaks are \n
, then this will work:
#(.*?)(\n|$)
Javascript:
str.replace(/#(.*?)(\n|$)/g,"<h1>$1</h1>")
^#\s(\w+)$
Will match any line beginning with a #, followed by a single whitespace character and then followed by at least 1 word character (A-Z, 0-9 and underscores). It then stores a match group of the headline text.
You should be able to call this match group with \1.