M
M
Michael R.2017-04-22 18:30:56
Django
Michael R., 2017-04-22 18:30:56

How to find and remove specific elements from text?

Hello!
It is necessary to find and delete elements on the page that match 'under the description': <p>&nbsp;</p>, <p></p>.
I can’t figure out in which direction to dig at all, I tried to do it through the regular season, but it didn’t really work out.

UPD (added to the question):
At the entrance we have: A page with text.
What you need: Find on the page <p>&nbsp;</p>and <p></p>, if similar is found, remove these elements from the page.

Plz let me know, thanks!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
adrewind, 2017-04-22
@Mike_Ro

If you want regular
But ideally, you need to parse the HTML code first. You can then search using css selectors or xpaths. If you are doing this in a browser then use document.querySelectorAll or Document.evaluate respectively.
xpath is better for your needs, use https://scrapinghub.github.io/xpath-playground/ to test your expression before use.

K
KnightForce, 2017-04-22
@KnightForce

More precisely?
I can only say that the regular expression is wrong.
I understand that I did not give an exact description in order to find solutions for all cases.
But, if you need to remove all identical elements, for example, remove all dashes from the string "1-2-3-4-5-6-7-8", then: . If the elements depend on something, then:"1-2-3-4-5-6-7-8".split("-").join("");

function removeEl(str, el) {
   str.split(el).join("");
}
removeEl("1-2-3-4-5-6-7-8", "-");

If the condition is not unambiguous, such as to remove dots or commas, then a regular expression.
var str = "1.2.3.4.5,6,7,8,9";
var newStr = str.replace(/[.,]/g, "");
console.log(newStr); // "123456789"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question