Remove link tag that have some URL with Cheerio











up vote
0
down vote

favorite












I have an HTML code that I am processing with the Cheerio library, I need to delete the tag "http://www.example.com'> example " for the links that refer to a domain (in this case "http://www.example.com") but not the other links. In addition, in the case of the link label being deleted, the keyword that contains it must be maintained.



Example origin:



Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.



Example result:



Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.



Thank you!










share|improve this question






















  • can you please share codepen or jsfiddle your html content
    – Deepak rao
    Nov 19 at 11:14















up vote
0
down vote

favorite












I have an HTML code that I am processing with the Cheerio library, I need to delete the tag "http://www.example.com'> example " for the links that refer to a domain (in this case "http://www.example.com") but not the other links. In addition, in the case of the link label being deleted, the keyword that contains it must be maintained.



Example origin:



Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.



Example result:



Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.



Thank you!










share|improve this question






















  • can you please share codepen or jsfiddle your html content
    – Deepak rao
    Nov 19 at 11:14













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an HTML code that I am processing with the Cheerio library, I need to delete the tag "http://www.example.com'> example " for the links that refer to a domain (in this case "http://www.example.com") but not the other links. In addition, in the case of the link label being deleted, the keyword that contains it must be maintained.



Example origin:



Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.



Example result:



Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.



Thank you!










share|improve this question













I have an HTML code that I am processing with the Cheerio library, I need to delete the tag "http://www.example.com'> example " for the links that refer to a domain (in this case "http://www.example.com") but not the other links. In addition, in the case of the link label being deleted, the keyword that contains it must be maintained.



Example origin:



Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.



Example result:



Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.



Thank you!







node.js cheerio






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 11:11









Noelia Romero

135




135












  • can you please share codepen or jsfiddle your html content
    – Deepak rao
    Nov 19 at 11:14


















  • can you please share codepen or jsfiddle your html content
    – Deepak rao
    Nov 19 at 11:14
















can you please share codepen or jsfiddle your html content
– Deepak rao
Nov 19 at 11:14




can you please share codepen or jsfiddle your html content
– Deepak rao
Nov 19 at 11:14












2 Answers
2






active

oldest

votes

















up vote
1
down vote













I found the solution to my problem:



$('a').each(function() {
if ($(this).attr("href").indexOf('example.com') > -1) {
$(this).replaceWith($(this).html());
}
});


If I use the .remove() function it delete the complete tag, but with this solution it only delete the link tag that contains example.com in the href attr.



I hope this helps to other people with the same challence. ;)






share|improve this answer





















  • Not bad, but use css whenever possible
    – pguardiario
    Nov 20 at 11:09


















up vote
0
down vote













It sounds like you want:



$('a[href*="www.example.com"]').remove()


or



$('a[href*="www.example.com"]').each((i, a) => $(a).replaceWith($(a).text()))





share|improve this answer























  • It works deleting the complete link, I want that but I need that the keyword doens't be deleted too.
    – Noelia Romero
    Nov 20 at 9:07










  • ok, try my edit.
    – pguardiario
    Nov 20 at 11:07











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373367%2fremove-link-tag-that-have-some-url-with-cheerio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













I found the solution to my problem:



$('a').each(function() {
if ($(this).attr("href").indexOf('example.com') > -1) {
$(this).replaceWith($(this).html());
}
});


If I use the .remove() function it delete the complete tag, but with this solution it only delete the link tag that contains example.com in the href attr.



I hope this helps to other people with the same challence. ;)






share|improve this answer





















  • Not bad, but use css whenever possible
    – pguardiario
    Nov 20 at 11:09















up vote
1
down vote













I found the solution to my problem:



$('a').each(function() {
if ($(this).attr("href").indexOf('example.com') > -1) {
$(this).replaceWith($(this).html());
}
});


If I use the .remove() function it delete the complete tag, but with this solution it only delete the link tag that contains example.com in the href attr.



I hope this helps to other people with the same challence. ;)






share|improve this answer





















  • Not bad, but use css whenever possible
    – pguardiario
    Nov 20 at 11:09













up vote
1
down vote










up vote
1
down vote









I found the solution to my problem:



$('a').each(function() {
if ($(this).attr("href").indexOf('example.com') > -1) {
$(this).replaceWith($(this).html());
}
});


If I use the .remove() function it delete the complete tag, but with this solution it only delete the link tag that contains example.com in the href attr.



I hope this helps to other people with the same challence. ;)






share|improve this answer












I found the solution to my problem:



$('a').each(function() {
if ($(this).attr("href").indexOf('example.com') > -1) {
$(this).replaceWith($(this).html());
}
});


If I use the .remove() function it delete the complete tag, but with this solution it only delete the link tag that contains example.com in the href attr.



I hope this helps to other people with the same challence. ;)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 9:19









Noelia Romero

135




135












  • Not bad, but use css whenever possible
    – pguardiario
    Nov 20 at 11:09


















  • Not bad, but use css whenever possible
    – pguardiario
    Nov 20 at 11:09
















Not bad, but use css whenever possible
– pguardiario
Nov 20 at 11:09




Not bad, but use css whenever possible
– pguardiario
Nov 20 at 11:09












up vote
0
down vote













It sounds like you want:



$('a[href*="www.example.com"]').remove()


or



$('a[href*="www.example.com"]').each((i, a) => $(a).replaceWith($(a).text()))





share|improve this answer























  • It works deleting the complete link, I want that but I need that the keyword doens't be deleted too.
    – Noelia Romero
    Nov 20 at 9:07










  • ok, try my edit.
    – pguardiario
    Nov 20 at 11:07















up vote
0
down vote













It sounds like you want:



$('a[href*="www.example.com"]').remove()


or



$('a[href*="www.example.com"]').each((i, a) => $(a).replaceWith($(a).text()))





share|improve this answer























  • It works deleting the complete link, I want that but I need that the keyword doens't be deleted too.
    – Noelia Romero
    Nov 20 at 9:07










  • ok, try my edit.
    – pguardiario
    Nov 20 at 11:07













up vote
0
down vote










up vote
0
down vote









It sounds like you want:



$('a[href*="www.example.com"]').remove()


or



$('a[href*="www.example.com"]').each((i, a) => $(a).replaceWith($(a).text()))





share|improve this answer














It sounds like you want:



$('a[href*="www.example.com"]').remove()


or



$('a[href*="www.example.com"]').each((i, a) => $(a).replaceWith($(a).text()))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 11:07

























answered Nov 19 at 23:01









pguardiario

35.6k978112




35.6k978112












  • It works deleting the complete link, I want that but I need that the keyword doens't be deleted too.
    – Noelia Romero
    Nov 20 at 9:07










  • ok, try my edit.
    – pguardiario
    Nov 20 at 11:07


















  • It works deleting the complete link, I want that but I need that the keyword doens't be deleted too.
    – Noelia Romero
    Nov 20 at 9:07










  • ok, try my edit.
    – pguardiario
    Nov 20 at 11:07
















It works deleting the complete link, I want that but I need that the keyword doens't be deleted too.
– Noelia Romero
Nov 20 at 9:07




It works deleting the complete link, I want that but I need that the keyword doens't be deleted too.
– Noelia Romero
Nov 20 at 9:07












ok, try my edit.
– pguardiario
Nov 20 at 11:07




ok, try my edit.
– pguardiario
Nov 20 at 11:07


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373367%2fremove-link-tag-that-have-some-url-with-cheerio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy