15

I want to replace every <li> tag with # & every </li> with <br />

I think I need a global replace function for that rather than a simple string.replace() so...

var s=obj1.innerHTML;

 s = s.replace(/<li>/g,&quot;# &quot;);
 s = s.replace(/</li>/g,&quot;<br/>&quot;);

But it doesn't seem to be working. Any mistake ?

EDIT: I am going to use the code in blogger & that is why it needs to be parsed.So that's why you see &quot; instead of ".

6 Answers 6

32

Do this way:-

LIVE DEMO

var s="<div>hello world <br/><li>First LI</li><li>Second LI</li></div>";   
s = s.replace(/<li>/g, "#");  
s = s.replace(/<\/li>/g,"<br/>"); 
alert(s);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @siva charan please tell me how can I delete the tag and whatever it contains in b/w. I am using something like this replace(/<title[^>]*>[\T\t]*?<\/title>/g,''); But not working. Much thanks
so you can just use your html tag inside the first replace arg. You only need to escape tag-end-slashes, not angle brackets. Like .replace(/<p><br><\/p>/g, "<p></p>")
7

Sorry but if the li tag has some attribute (like style) you will have text that you don't want. For me the simplest way is using regular expressions like this:

var regex = /(<li[^>]+>|<li>|<\/li>)/g;
s = s.replace(regex , ' ');

In the case you want to strip all the <tags> </tags> (not only li tags) you should do something like this:

var regex = /(<[^>]+>|<[^>]>|<\/[^>]>)/g;
s =s.replace(regex , ' ');

2 Comments

you can use this one /(<li [^>]+>|<li>|<\/li>)/g (the same but with an space) and it will go fine for you.
Yes, I had modified it like this /(<li >|<li [^>]+>|<li>|<\/li>)/g already, with the first <li > ensuring tags with only a single additional space are replaced as well.
6
obj1.innerHTML = obj1.innerHTML.replace(/<li>/g, '#').replace(/<\/li>/g, '<br>');

You just have to escape the / in /li otherwise it will be taken as a regex delimiter.

Comments

1

This works just fine:

<html>
<head>
    <script type="text/javascript" src="http://streamed.in/static/js/jquery-1.4.1.min.js"></script>
    <script>
    $(document).ready(function(){
        var s = $('#obj1').html();
        s = s.replace(/<li>/g,'#');
        s = s.replace(/<\/li>/g,'<br/>');
        $('#obj1').html(s);
    });
    </script>
</head>
<body id="obj1">
    <li>omg 1</li>
    <li>omg 2</li>
    <li>omg 3</li>
    <li>omg 4</li>
</body>
</html>

or you can try and replace your second replace line with:

s.replace(/<\/li>/g,'<br/>');

Comments

0

Pretty late to the party, but all the previous answers are using RegExp(s) and you don't need Regular expressions to replace an HTML tag with another one:

Simple Example - Replace h1 with h2

const oldElement = document.querySelector('h1');

// Create a new element
const newElement = document.createElement('h2');
newElement.textContent = oldElement.textContent; // Copy content from old element to new one

// Replace old element with the new one
oldElement.parentNode.replaceChild(newElement, oldElement);

Answer to the question

function replaceLiTags() {
  const HTMLElementsLi = document.querySelectorAll('li');

  [...HTMLElementsLi].forEach(el => {
    el.parentNode.append("#" + el.textContent);
    el.parentNode.append(document.createElement('br'))
    el.remove();
  });
  
}
<ul>
  <li>test 1</li>
  <li>test 2</li>
  <li>test 3</li>
</ul>

<button onclick="replaceLiTags()">replace tags</button>

NOTE: you should always have LI as children of UL. So what you trying to achieve is not syntactically correct since according to W3C lists are made up of sequences of list items defined by the LI element.

Comments

-4

If you can use jQuery, this would help you:

$("li").after("#<br/>").remove();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.