admin管理员组文章数量:1414621
I have the following object:
{
"_id": "59f6f931d20f73000410bbd8",
"title": "Test",
"salary": "1337",
"maxSalary": "4000",
"minSalary": "1500",
"introText": "Lorem __Ipsum__ Sit Dolor Amet",
"expectText": "Lorem Ipsum Sit _Dolor__ Amet Est __Circa__."
}
In my Vue.js app I implemented vue-markdown
to render the markdown to display bold and cursive words.
What I now have to implement is, to parse the object for every __word __ and replace it with regex to word .
What I got so far:
let objJson = JSON.stringify(obj);
objJson = objJson.replace(/\_/g, '');
let jobXML = JSON.parse(objJson);
res.send(jobXML);
So this is my workaround to delete the "__" characters but I did not find any other resource which explains how to replace it with my HTML entity.
My workaround is needed because I use /jobs
to display all the jobs in my vue app (which can use markdown), but I have also another express route that can not use "__" but needs the HTMl entities.
I have the following object:
{
"_id": "59f6f931d20f73000410bbd8",
"title": "Test",
"salary": "1337",
"maxSalary": "4000",
"minSalary": "1500",
"introText": "Lorem __Ipsum__ Sit Dolor Amet",
"expectText": "Lorem Ipsum Sit _Dolor__ Amet Est __Circa__."
}
In my Vue.js app I implemented vue-markdown
to render the markdown to display bold and cursive words.
What I now have to implement is, to parse the object for every __word __ and replace it with regex to word .
What I got so far:
let objJson = JSON.stringify(obj);
objJson = objJson.replace(/\_/g, '');
let jobXML = JSON.parse(objJson);
res.send(jobXML);
So this is my workaround to delete the "__" characters but I did not find any other resource which explains how to replace it with my HTML entity.
My workaround is needed because I use /jobs
to display all the jobs in my vue app (which can use markdown), but I have also another express route that can not use "__" but needs the HTMl entities.
- 1 So ... what you're asking for is a Markdown-to-HTML renderer? – melpomene Commented Oct 30, 2017 at 13:58
4 Answers
Reset to default 1I think I got a working solution on my own:
let objJson = JSON.stringify(obj);
let regexBold = /\_\_(\S(.*?\S)?)\_\_/gm;
let regexItalic = /\_(\S(.*?\S)?)\_/gm;
objJson = objJson.replace(regexBold, '<strong>$1</strong>');
objJson = objJson.replace(regexItalic, '<i>$1</i>');
let jobXML = JSON.parse(objJson);
res.send(jobXML);
Would be still nice to see other/better approaches for that problem!
This is the regex you need to use in your replace call:
str.replace(/_{1,}([ a-z0-9]+)_{1,}/img, "<strong>$1</strong>")
Demo:
var str = "Lorem Ipsum Sit _Dolor__ Amet Est __Circa__.";
console.log(str.replace(/_{1,}([ a-z0-9]+)_{1,}/img, "<strong>$1</strong>"));
Probably you can try this one:
str.replace(/__(.*?)__((_+|\W+|$))/g, '<strong>$1</strong>$2')
My ultimate (may have uncovered cases) RegExp for cleaning all markdown inline markings:
/(?<marks>[`]|\*{1,3}|_{1,3}|~{2})(?<inmarks>.*?)\1|\[(?<link_text>.*)\]\(.*\)/g
Sample Markdown line:
Markdown with a `code` *test* with **striping** all _Markdown_ and [Some awesome link 123](http://test./a?rt&=123&test=this+open) and more `code` more __fenced 2__ and some ~~strike and more strike~~ !!!
We can replace all markings and link with this replace code:
line.replace(re, '$<inmarks>$<link_text>')
var line = "Markdown with a `code` *test* with **striping** all _Markdown_ and [Some awesome link 123](http://test./a?rt&=123&test=this+open) and more `code` more __fenced 2__ and some ~~strike and more strike~~ !!!";
document.querySelector('h3:nth-of-type(1)+pre').textContent = line;
var re = /(?<marks>[`]|\*{1,3}|_{1,3}|~{2})(?<inmarks>.*?)\1|\[(?<link_text>.*)\]\(.*\)/g;
var fixed = line.replace(re, "$<inmarks>$<link_text>");
document.querySelector('h3:nth-of-type(2)+pre').textContent = fixed;
pre { white-space: pre-wrap; }
<h3>Before</h3>
<pre></pre>
<h3>After</h2>
<pre></pre>
And if we want to have the link text with some begin/end character, like a double quote, we can use a function to perform the replacement like this:
line.replace(re, (match, marks, inMarks, linkText) => {
return linkText && `"${linkText}"` || inMarks || match;
})
var line = "Markdown with a `code` *test* with **striping** all _Markdown_ and [Some awesome link 123](http://test./a?rt&=123&test=this+open) and more `code` more __fenced 2__ and some ~~strike and more strike~~ !!!";
document.querySelector('h3:nth-of-type(1)+pre').textContent = line;
var re = /(?<marks>[`]|\*{1,3}|_{1,3}|~{2})(?<inmarks>.*?)\1|\[(?<link_text>.*)\]\(.*\)/g;
var fixed = line.replace(re, (match, marks, inMarks, linkText) => {
return linkText && `"${linkText}"` || inMarks || match;
});
document.querySelector('h3:nth-of-type(2)+pre').textContent = fixed;
pre { white-space: pre-wrap; }
<h3>Before</h3>
<pre></pre>
<h3>After</h2>
<pre></pre>
本文标签: javascriptReplace markdown characters with regexStack Overflow
版权声明:本文标题:javascript - Replace markdown characters with regex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745187251a2646741.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论