So, I’ve got two problems with my javascript code. I’ll try to make it as short as possible and only copy parts that are necessary for the problem.
I have this html code (it’s not my fault that every li looks different):
<li>
<span style="font-family:arial, helvetica, sans-serif;">Siehe <a href="Link 1" rel="">Text 1</a>.</span>
</li>
<li>
<span style="font-family:arial, helvetica, sans-serif;">Siehe</span> <a href="Link 1" rel="">Text 2</a>.<span style="font-family:arial, helvetica, sans-serif;"> </span>
</li>
<li>
Siehe <a href="Link 1" rel="">Text 3</a>.
</li>
And here comes the necessary javascript code for my problems. First, I delete every span element. After that I want to replace the "a href" parts and increase the number of the new string.
var neu = document.getElementById("ausgabe").value;
var nummer = 0;
function getNextDlcNumber() {
return ++nummer;
}
var ersetzen = neu
.replace(/<span.*?>(.*?)</span>/g, "$1") // Gets rid of every span
.replace(/s<li>nssSiehes*<a href=".*?" rel="">(.*?)</a>.ns</li>/g, "t<li>nttSiehe <a class="trophiesAnkerLink" href="#dlc" + getNextDlcNumber() + "" rel="">$1</a>.nt</li>")); // Replaces a href with increasing dlc numbers
document.getElementById("ausgabe").value = ersetzen;
There are two problems with this:
-
The first and third "a href" are replaced successfully. But the second one isn’t and I can’t figure out why. I think it has something to do with the span tag between "Siehe" and the start of the "a" tag, however the span should get deleted due to the first replace so I’m not sure if that’s what causes the problem. I tried using "Siehes*<a", "Siehe <a" as well as an or using | that contained the "
</span>
" but none of that worked. -
The number after "#dlc" isn’t increasing, it just stays at "#dlc1" even for the third one. Do I need to do that in a different way?
Here’s the output after using the js:
<li>
Siehe <a class="trophiesAnkerLink" href="#dlc1" rel="">Text 1</a>.
</li>
<li>
Siehe <a href="Link 1" rel="">Text 2</a>.
</li>
<li>
Siehe <a class="trophiesAnkerLink" href="#dlc1" rel="">Text 3</a>.
</li>
I hope I posted every necessary information. If not, I can provide more. Thanks for your help in advance 🙂
3
Answers
Try using
neu.replaceAll(pattern, replacement)
.Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
Actually it’s a very bad idea to manually parse and edit DOM elements contents, you don’t have to complicate your life and reinvent the wheel.
You can use JS built-in methods to directly access
.href
attribute of your link elements and change it directly, without even using.replace()
method.This is how can be your code:
You can see it can just be a one liner code, to do all what you were trying to achieve.
Demo:
You can check the new values of your
links
by hovering them, et voilà .As others have stated in comments and answers, using the DOM to parse the value in the
textarea
is much easier to generate the output you want. Below are two versions (DOM and text parsing) so that you can compare the simplicity of using DOM parsing versus text parsing.(Both code versions can be rewritten and condensed. They are verbose to make the samples easier to debug.)
DOM parsing version:
The text parsing version for the same output: