looking for keywords that appear in a link or in a text.

Support
  • I want to use JavaScript to solve the problem of finding keywords on a web page. I open a web page and want to check 3 requirements:

    1. Does the keyword appear in the anchor text or title of any link?
    2. Does the keyword appear in the text outside the links on the page?
    3. Case sensitivity should be ignored.

    I have tried many different ways, but I still encounter an issue with the following case:

    Example text to check: "Vietnam"
    Keyword to check: "vi"
    The methods I tried all resulted in: "true"

    However, I want the result to be "false" when the keyword is a part of another word. Please help me.

    // Lấy nội dung của trang web và chuyển về chữ thường
    var pageContent = document.body.innerHTML.toLowerCase();

    // Từ khoá cần kiểm tra và chuyển về chữ thường
    var keyword = [[KEY_SEARCH]];

    // Tạo biểu thức chính quy để kiểm tra từ khoá trong các liên kết và anchor text
    var linkRegex = new RegExp(>[^<]\b${keyword}\b[^<]<a[^>]>|>[^<]\b${keyword}\b[^<]*</a>, 'ig');

    // Kiểm tra từ khoá trong các liên kết và anchor text
    var keywordInLinks = linkRegex.test(pageContent);

    // Loại bỏ các thẻ <a> sau khi đã kiểm tra
    var pageContentWithoutLinks = pageContent.replace(linkRegex, ">");

    // Kiểm tra từ khoá trong văn bản ngoài các liên kết (với word boundary)
    var keywordInText = new RegExp(\b${keyword}\b, 'g').test(pageContentWithoutLinks);

    [[XUAT_HIEN_TRONG_LINK]] = keywordInLinks
    [[XUAT_HIEN_TRONG_VAN_BAN]] = keywordInText