如何用 SPAN 标签替换特定的文本换行字符?



期望的行为

::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>

此外,像http:这样的输入会转换为http::

编辑:

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

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

使用正则表达式/::([^::]+)::/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>"));

这很简单。这个对我有用:(::)([ws]+)(::)<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.

最新更新