Skip to content
Advertisement

Appending text to a string if it matches a condition

I am learning to scrape websites. I need to get document titles and links to them, I already manage to do this, but the format of the resulting links is sometimes not what I need. Here is a snippet of the information I get:

JavaScript

You can see that in the second case I get only part of the link, while in the first I get the whole link. To the format of the second link, I need to add a part of the text that I know in advance. But this must be done on the basis of the condition that the format of this link will be defined. That is, at the output, I want to receive the following:

JavaScript

How should I do it? Here is the previously reproduced code:

JavaScript

Advertisement

Answer

You can check if the string has an scheme, and if not add it and also the host:

JavaScript

Full working code:

JavaScript

Research:

Advertisement