Link to home
Start Free TrialLog in
Avatar of Alex Lord
Alex Lord

asked on

Remove black list object <li> </l> <li> 1 </li>

<p>	bullet point bug test</p><ul><li>	</li><li>		1	</li><li>		2	</li><li>		3	</li><li>		4	</li><li>		5</li></ul>

Open in new window



so i have a string from CKEDTIOR ,

it keeps adding in blank bullet points,

is their a way to remove the <li> </li> if it has no value inside it ?

for example keep the li with 1 , 2 ,3 but remove the first one which is blank ?
Avatar of Rikin Shah
Rikin Shah
Flag of India image

Hi,

See below-
  var text = '<p>	bullet point bug test</p><ul><li>	</li><li>		1	</li><li>		2	</li><li>		3	</li><li>		4	</li><li>		5</li></ul>';
  var result = text.replace(/(<li>)\t+(<\/li>)/g, '');

  alert(result);

Open in new window

This one will cover white spaces and tabs as well-

  var text = '<p>	bullet point bug test</p><ul><li>	</li><li>		1	</li><li>		2	</li><li>		3	</li><li>		4	</li><li>		5</li></ul>';
  var result = text.replace(/(<li>)[\t|\s\s+](<\/li>)/g, '');

  alert(result);

Open in new window

You could just replace them out
var html = "<ul><li>	</li><li>		1	</li><li>		2	</li><li>		3	</li><li>		4	</li><li>		5</li></ul>";
var html = html.replace(/<li>\s*<\/li>/g,'');

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To add to what Julian said, Rikin also has confused character classes with groups. In his second expression the "|" and "+" would be treated as literal characters.
https://www.regular-expressions.info/charclass.html
https://www.regular-expressions.info/brackets.html