OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Multi tag replace between values (Javascript)

  • Thread starter Thread starter Cove
  • Start date Start date
C

Cove

Guest
I have some script which makes replacements. It works fine, but because of replaced values standing close to each other i've got more than one
between values.

It looks like:

Code:
<br />
Value

<br />
Value

<br />


<br />
Value

<br />
Value

<br />


<br />
Value

<br />


<br />

<br />

My task is to get only one <br> between values:

Code:
<br />
Value

<br />
Value

<br />
Value

Because of not so many variations in my list I know one decision for all available variants but it is bad hardcode something like:

Code:
var mystring=`<br />


<br />`

for (i=0; i<mystring.length; i++){
          var regex = new RegExp(mystring[i],"g");
           mystring = mystring.replace(regex, "");
       }

Is there any elegant way to solve my case? Thinking of regexp but it's too hard for me to write it. Or may be there is another way?

<p>I have some script which makes replacements. It works fine, but because of replaced values standing close to each other i've got more than one <br> between values.</p>
<p>It looks like:</p>
<pre><code><br />
Value

<br />
Value

<br />


<br />
Value

<br />
Value

<br />


<br />
Value

<br />


<br />

<br />
</code></pre>
<p>My task is to get only one <code><br></code> between values:</p>
<pre><code><br />
Value

<br />
Value

<br />
Value
</code></pre>
<p>Because of not so many variations in my list I know one decision for all available variants but it is bad hardcode something like:</p>
<pre><code>var mystring=`<br />


<br />`

for (i=0; i<mystring.length; i++){
var regex = new RegExp(mystring,"g");
mystring = mystring.replace(regex, "");
}
</code></pre>
<p>Is there any elegant way to solve my case? Thinking of regexp but it's too hard for me to write it. Or may be there is another way?</p>
 
Top