javascript - 正则表达式替换为原型(prototype)?

标签 javascript regex prototype

我得到了一些按以下方式格式化的 html:

[Title|<a class="external" href="http://test.com">http://test.com</a>]

根据这些文本,我想使用“标题”作为文本和“http://test.com”作为链接来创建链接。我怎样才能最好地在原型(prototype)中做到这一点?

最佳答案

纯正则表达式:

var ProperLink=WierdString.replace(/\[([^|]+)\|(<[^>]+>)[^<]+[^\]]+\]/,'$2$1</a>')

在您提供的上下文中:

function convert(id){
    $(id).innerHTML=$(id).innerHTML.replace(/\[([^|]+)\|(<[^>]+>)[^<]+[^\]]+\]/g,'$2$1</a>');
}
convert('testdiv');

关于javascript - 正则表达式替换为原型(prototype)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5228698/

相关文章:

javascript - 在 KnockoutJS ViewModel 上定义函数

regex - sed 以匹配多行范围并替换

regex - Linux bash 中文件名中的时间戳匹配正则表达式的问题

javascript - 无法弄清楚如何确保最后的模式

javascript - 从对象本身访问对象属性

javascript - 能够在定义之前使用函数表达式

javascript - 如何向下滚动到选择元素中的特定选项?

javascript - 事件处理麻烦JS

javascript - 关于 JavaScript 软件分解结构指南

c - 原型(prototype) "const int* foo(int)"是什么意思,特别是与 "int* foo(int)"相比?我只理解第二个