gpt4 book ai didi

javascript - 用链接替换 ​​html 中的单词,但如果搜索词冲突,则更喜欢更长的单词

转载 作者:行者123 更新时间:2023-12-03 23:41:43 26 4
gpt4 key购买 nike

我在执行以下任务时遇到问题:
我需要通过将它包含的一些单词链接到“阅读更多”页面来丰富包含 html 的字符串。不幸的是,当我要替换的搜索词开始重叠时,它变得很棘手。
我要么以嵌套链接结束,要么像下面的示例中那样,更长的术语不会被替换。

let html = `<p>So there is this Text in HTML containing words.</p>
<p>I want to link some special words to their read more pages.</p>
<p>But if search terms contain each other they re not linked correctly. Like the ones above.</p>`;

const linkTo = [
{term: 'words', link: 'https://example.com/words'},
{term: 'special words', link: 'https://example.com/special_words'},
];

for(const pair of linkTo){
const re = new RegExp(pair.term, "g");
html = html.replace(re, `<a href="${pair.link}">${pair.term}</a>`);
}

document.body.innerHTML = html;
<html><body></body></html>

在上面的示例中,第 2 行中的“特殊词”应该链接到它自己的页面。
有任何想法吗?
提前致谢!

最佳答案

您需要按长度按降序对键进行排序,并从术语中创建一个正则表达式:

let html = `<p>So there is this Text in HTML containing words.</p>
<p>I want to link some special words to their read more pages.</p>
<p>But if search terms contain each other they re not linked correctly. Like the ones above.</p>`;

const linkTo = [
{term: 'words', link: 'https://example.com/words'},
{term: 'special words', link: 'https://example.com/special_words'},
];
const keys = linkTo.map(x => x.term).sort((a, b) => b.length-a.length)
const re = new RegExp("\\b(?:" + keys.join("|") + ")\\b", "g");
html = html.replace(re, (m) => `<a href="${linkTo.find(x => m == x.term).link}">${m}</a>`);
document.body.innerHTML = html;
<html><body></body></html>

请注意,我暂时不会转义特殊字符,因为您的所有术语都由单词字符组成,这就是我添加 \b 的原因在两端,字边界。
如果您需要搜索可能包含特殊字符的术语,您将需要使用
const keys = linkTo.map(x => x.term.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).sort((a, b) => b.length-a.length)
const re = new RegExp("(?<!\\w)(?:" + keys.join("|") + ")(?!\\w)", "g");
更多细节:
  • linkTo.map(x => x.term).sort((a, b) => b.length-a.length) - 获取 terms来自 linkTo项目并按长度按降序对它们进行排序,越长的优先。这是必要的,因为 The Regex Engine Is Eager第一个替代方案“获胜”并使正则表达式引擎停止处理其他替代方案
  • new RegExp("\\b(?:" + keys.join("|") + ")\\b", "g")构建一个像 \b(?:special words|words)\b 这样的正则表达式
  • .replace(re, (m) => `<a href="${linkTo.find(x => m == x.term).link}">${m}</a>`) - 只解析一次字符串,并用相应的link 替换每个匹配项.

  • 特殊字符支持解决方案:

    let html = `<p>So there is this Text in HTML containing words.</p>
    <p>I want to link some special words to their read more pages.</p>
    <p>But if search terms contain each other they re not linked correctly. Like the ones above.</p>`;

    const linkTo = [
    {term: 'words', link: 'https://example.com/words'},
    {term: 'special words', link: 'https://example.com/special_words'},
    ];
    const keys = linkTo.map(x => x.term.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).sort((a, b) => b.length-a.length)
    const re = new RegExp("(\\W|^)(" + keys.join("|") + ")(?!\\w)", "g");
    html = html.replace(re, (x,y,z) => `${y}<a href="${linkTo.find(i => z == i.term).link}">${x}</a>`);
    document.body.innerHTML = html;

    关于javascript - 用链接替换 ​​html 中的单词,但如果搜索词冲突,则更喜欢更长的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65341353/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com