我得到了一些按以下方式格式化的 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/