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!
node.js cheerio
add a comment |
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!
node.js cheerio
can you please share codepen or jsfiddle your html content
– Deepak rao
Nov 19 at 11:14
add a comment |
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!
node.js cheerio
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
node.js cheerio
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
add a comment |
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
add a comment |
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. ;)
Not bad, but use css whenever possible
– pguardiario
Nov 20 at 11:09
add a comment |
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()))
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
add a comment |
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. ;)
Not bad, but use css whenever possible
– pguardiario
Nov 20 at 11:09
add a comment |
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. ;)
Not bad, but use css whenever possible
– pguardiario
Nov 20 at 11:09
add a comment |
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. ;)
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. ;)
answered Nov 20 at 9:19
Noelia Romero
135
135
Not bad, but use css whenever possible
– pguardiario
Nov 20 at 11:09
add a comment |
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
add a comment |
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()))
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
add a comment |
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()))
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
add a comment |
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()))
It sounds like you want:
$('a[href*="www.example.com"]').remove()
or
$('a[href*="www.example.com"]').each((i, a) => $(a).replaceWith($(a).text()))
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
can you please share codepen or jsfiddle your html content
– Deepak rao
Nov 19 at 11:14