Is This Javascript Replace Possible?

September 24, 2010 at 08:41:21
Specs: Windows XP
I am trying to create a script that will clean up unnecessary HTML tags in a text string, caused by users playing about with fonts etc.

An example would be starting with a string like this:

aaa<span style="font-size: larger">bbb</span>ccc

I can strip out the font using:

$html_string_new = $html_string.replace(/font-size.*;/g, "");

which leaves me with:

aaa<span style="">bbb</span>ccc

That has basically achieved what I wanted to do, however is the <span> tags have nothing left active, I would like to remove them, leaving the string as:


Is there a way to scan for <span style="">*<\/span> and to retain the code that is in the middle (in this case "bbb")?

I am not a Javascript expert - at least not yet!

See More: Is This Javascript Replace Possible?

Report •

September 26, 2010 at 22:20:16
Well, matching the content inbetween is the hard part, you could strip ALL span tags (leaving middle content) with something like this:


Should remove any <span...> or </span> tag


You might also be able to do this easier with jQuery, maybe something like:

$('span[style=""]').each(function() {
  $(this).replaceWith( $(this).text() );

(Note I didn't test either of those, so could be typos...)

Also - CKEditor/FCKEditor have options for Removing Formatting, (I think you click the 'Remove Formatting' button) and it will strip predefined tags (leaving content intact). You might look into that if you're using a WYSIWYG editor.

Report •

September 27, 2010 at 11:11:14
These sound like good solutions, thanks.

Actually the string I am trying to string the tags from is within FCKEditor, so I might even be able to use that.

Really appreciate the help. I can easily try these to see if they work.

This is all because some text boxes created in GCKEditor, which contain multiple tables as well, ended up with multiple FONT tags, so the HTML is far more complex and larger than it needs to be, and now I have cleaned up the software where the text is used, there is really no need to change font size.

Report •
Related Solutions

Ask Question