Replacement problem

I didn’t find a suitable category for this post.

Is there any way or program to solve this kind of replacement. I have the text file which contains:

<strong>Car</strong><br><a href="https://www.google.fi/search?q=&amp"></a> 

<strong>House</strong><br><a href="https://www.google.fi/search?q=&amp"></a> 

<strong>Lady</strong><br><a href="https://www.google.fi/search?q=&amp"></a> 

After replacement text should look like this:

<strong>Car</strong><br><a href="https://www.google.fi/search?q=Car&amp"></a> 

<strong>House</strong><br><a href="https://www.google.fi/search?q=House&amp"></a> 

<strong>Lady</strong><br><a href="https://www.google.fi/search?q=Lady&amp"></a> 

Is it possible to make all replacements AT ONCE? And how?

I know I have to use FOR/NEXT or DO WHILE/ENDDO loop but I don’t know how.

I use Notepad++, but I can use any other program if I can solve this problem.

Thanks if you can help.

There’s probably a more elegant way but this works…

// Find all links..
let allLinks = document.querySelectorAll("a");

// Loop through all links, find the text of the element two elements prior, and insert the text into the href.
allLinks.forEach(a => {
  const strongText = a.previousSibling.previousSibling.innerText;
  a.href = a.href.replace("q=", "q=" + strongText);
});

Thanks for the answer but I don’t know how to use your script. I don’t even know what is that script language :slight_smile:

I’d be grateful if you could tell how to use this script with Notepad++ or any other text editor.

I don’t think this is possible to do with any editor. You need some kind of program/script which is doing that for you.
Maybe the easiest way is to use Excel and just write a small VBA macro. But at the end you need some programming skills to make this happen.

I’m sorry. When you talked about FOR/NEXT and DO/WHILE, I thought that you were looking for a scripted solution.

The one I gave you was pure javascript. All you would need to do is add this to the bottom of your page (you could put this right before the </body> closing tag)

<script>
// Find all links..
let allLinks = document.querySelectorAll("a");

// Loop through all links, find the text of the element two elements prior, and insert the text into the href.
allLinks.forEach(a => {
  const strongText = a.previousSibling.previousSibling.innerText;
  a.href = a.href.replace("q=", "q=" + strongText);
});
</script>

If you want to do it in notepad++, you’d have to use some sort of regular expression (not my strong suit) on a find/replace

I did actually post this yesterday, but thought I might have got the wrong end of the stick

If you go to search → replace

Put this in the find box

<strong>([^<]+)<\/strong><br><a href="https:\/\/www\.google\.fi\/search\?q=&amp"><\/a>

And this in the replace box

<strong>$1</strong><br><a href="https://www.google.fi/search?q=$1&amp"></a>

Make sure search mode is set to regular expression and hit replace all

The expression could be refactored, but seems to work.

3 Likes

Why refactor something you’ll literally run once? If it works it works :man_shrugging:

2 Likes

Are you giving me a hard time again :biggrin: Agreed though!

2 Likes

That’s called training :lol:

1 Like

Thank you to all of you, I use this regex find and replace in Notepad++ it works fine. Thanks!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.