替换要链接的JavaScript短代码

问题描述 投票:-3回答:2

我有一个包含一些文本和简码的网页。我想替换链接的短代码:

[a href="http://www.myurl.com" title="A Link Title"]My link text[/a] and even here [a href="www.yoururl.com" title="Another link" class="linkClass"]This is cool to[/a] !

为此

<a href="http://www.myurl.com" title="A Link Title">My link text</a> and even here <a href="www.yoururl.com" title="Another link" class="linkClass">This is cool to</a> !

您能帮我用JQuery或Javascript做到这一点吗?

谢谢您的帮助。

javascript jquery shortcode
2个回答
0
投票

尝试使用替换。希望它会有所帮助。

function sayHello() {
   var str = '[a href="http://www.myurl.com" title="A Link Title"]My link text[/a] and even here [a href="www.yoururl.com" title="Another link" class="linkClass"]This is cool to[/a] !';

   while(str.includes("[")){
      str = str.replace("[", "<");
   }

   while(str.includes("]")){
      str = str.replace("]", ">");
   }

   console.log(str);
}
sayHello();

0
投票

我终于找到了如何使用此脚本执行此操作:https://github.com/nicinabox/shortcode.js

这是我的代码来处理我的案件:

new Shortcode(document.querySelector('body'), {
  a: function(done) {
    var ret='<a';
    if (typeof this.options.href !== 'undefined' && this.options.href !== null) ret+=' href="'+this.options.href+'"';
    if (typeof this.options.class !== 'undefined' && this.options.class !== null) ret+=' class="'+this.options.class+'"';
    if (typeof this.options.title !== 'undefined' && this.options.title !== null) ret+=' title="'+this.options.title+'"';
    if (typeof this.options.style !== 'undefined' && this.options.style !== null) ret+=' style="'+this.options.title+'"';
    if (typeof this.options.target !== 'undefined' && this.options.target !== null) ret+=' target="'+this.options.title+'"';
    ret+='>'+this.contents+'</a>';
    return ret;
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.