0

期望的行为

替换::text::<span>text</span>

例如,给定一个字符串,如:

Here is a ::test span:: and another ::span::, colons in this context eg: 
http://somelink.com are not effected.  

如何用这样的打开和关闭标签::替换字符:<span>

Here is a <span>test span</span> and another <span>span</span>, colons 
in this context eg: http://somelink.com are not effected.  

我试过的

我实际上是在尝试复制以下行为:

https://www.npmjs.com/package/markdown-it-span

一个非常有用的markdown-it 解析器的插件,因为它似乎有一个错误(我无法弄清楚如何修复),这已在此处的一个问题中进行了讨论

代码中有一个错误导致创建 span 标签两次。

::spanned:: => <span><span>spanned</span></span>

此外,输入 likehttp:被转换为http::.

编辑:

我刚刚在这里遇到了类似的问题,可能有答案...

用 JavaScript 替换字符之间的所有内容

4

2 回答 2

2

使用 regex 似乎很简单/::([^::]+)::/g

var s = 'Here is a ::test span:: and another ::span::, colons in this context eg: http://somelink.com are not effected.';

console.log(s.replace(/::([^::]+)::/g, "<span>$1</span>"));

于 2018-10-13T06:37:07.337 回答
0

It's a simple enough. This one worked for me: (::)([\w\s]+)(::) replace it with <span>$2</span>.

::text::    => <span>text</span>
::spanned:: => <span>spanned</span>


Here is a ::test span:: and another ::span::, colons in this context eg: 
http://somelink.com are not effected.  
=> 
Here is a <span>test span</span> and another <span>span</span>, colons in this 
context eg: 
http://somelink.com are not effected.
于 2018-10-13T06:59:53.110 回答