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

时间:2018-10-13 06:28:37

标签: javascript regex markdown-it

期望的行为

::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 parser插件,因为它似乎有一个错误(我不知道如何解决),该问题已经discussed in an issue here

  

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

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

此外,将类似http:的输入转换为http::

编辑:

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

Replace all content between characters with JavaScript

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

这很简单。这个对我有用:public class Java_lesson_1 { public static void main(string[]arguments) { system.out.println("hello world") } } 替换为(::)([\w\s]+)(::)

<span>$2</span>