[英]regex that ignore all inside <a> [url][img]
This is a tricky regex code, actually i have a script that works fine, but i need a more precise pattern to avoid conflict with url that contain hashtag and bbcode.这是一个棘手的正则表达式代码,实际上我有一个可以正常工作的脚本,但我需要一个更精确的模式以避免与包含标签和 bbcode 的 url 冲突。
actually my code replace all hashtags words like #example and turn it into link like:实际上,我的代码替换了所有主题标签,例如 #example 并将其转换为链接,例如:
https://myurl/?q=example
so my regex is:所以我的正则表达式是:
const regex = /#[^\s.@#$%^&*()=+,\/;\[{\]}:?'";><]+/g;
i want a regex that ignore all urls inside <a>
, all url inside [url], and [img] to avoid bbcode conflict.我想要一个忽略<a>
中的所有 url、[url] 和 [img] 中的所有 url 和 [img] 的正则表达式,以避免 bbcode 冲突。
but i can't find a valid pattern但我找不到有效的模式
full code that i have actually:我实际上拥有的完整代码:
export default function () {
const regex = /#[^\s!@#$%^&*()=+.\/,\[{\]};:'"?><]+/g;
const p = this.$('.Post-body');
const baseurl = app.forum.attribute('baseUrl');
p.html = p.html(p.html().replace(regex, match => `<a href="${baseurl}/?q=${match}" class="hashlink" title="Find more post with this hashtag">${match}</a>`))
}
actually i've found a solution that works, i will post here the soution (could be useful for someone)实际上我找到了一个可行的解决方案,我会在这里发布解决方案(可能对某人有用)
const regex = /(?<!https?:\/\/\S*)#[^\s!@#$%^&*()=+.\/,\[{\]};:'"?><]+/g;
and i've removed the extra # using match.slice
我已经使用match.slice
删除了多余的 #
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.