Safe regexs to clean serialized DOM?
I'm after several, really safe regex patterns for JavaScript's .replace method. The input is a serialized DOM string, and I am wanting to remove all YUI3 classNames and YUI3 generated id attributes.
var resourceDOMStr = Y.DataType.XML.format( Y.Node.getDOMNode(this.getIframeDOMContainer()).innerHTML );
alert('unsanitized markup:\n\n'+resourceDOMStr );
// Remove YUI-added id's and classes
// regex to remove ' id="*"'
// regex to remove entire class attr: ' class="'yui3-*'"'
// regex to remove className + trailing space: class="'yui3-* 'safeClass"
// regex to remove className + leading space: class="safeClass' yui3-*'"
resourceDOMStr.replace('', '');
alert('sanitized markup:\n\n'+resourceDOMStr );
So yeah, I'd like to be clean and remove the entire id attribute, whose value will always begin with 'yui_3', eg; id="yui_3_3_0_1_1296949124608175"
. Also, I want to remove an entire class attribute if the only class it has is a YUI3-generated className, otherwise I just want to remove the YUI3 className and any leading/trailing spaces. The generated classNames will always begin with 'yui3-', examples;
class="yui3-dd-shim"
class="safeClass yui3-dd-shim"
class="yui3-dd-shim safeClass"
...where I don't want 'safeClass' to be altered, and I don't want a build-up of leading/trailing spaces, as the resulting replaced String will be loaded, cleaned and saved many times over.
Many thanks for any headache-solvers. d
EDIT:
<div id="wrap"><h1 id="yui_3_3_0_1_1296942015298202" class="yui3-dd-drop">Resource 1 Title</h1>
<p id="yui_3_3_0_1_1296942015298219" class="yui3-dd-drop">Lorem ipsum dolor sit amet, <a href="javacript:;" id="yui_3_3_0_1_1296942015298236" class="yui3-dd-drop">consectetur adipiscing</a> elit. Proin et sem leo, sed luctus nisi. Suspendisse pharetra iaculis laoreet. Pellentesque vulputate malesuada auctor. Integer laoreet ultricies nunc facilisis adipiscing.</p>
<div class="widget revealer">
<p>Revealer widget.</p>
<script type="text/javascript">
document.RevealerConfig = true;
</script>
</div>
<div class="widget quiz safeClass" id="safeId">
<p>Quiz widget.</p>
<script type="text/javascript">
document.QuizConfig = true;
</script>
</div>
<div class="snippet yui3-dd-drop" id="yui_3_3_0_1_1296942015298253">
Vestibulum fermentum, justo id porta suscipit, velit lorem hendrerit nisi, id tincidunt lectus ante quis lacus. Proin et erat sit amet turpis euismod dictum vitae a metus.
<div class="widget table">
<p>Table widget.</p>
<table width="80%" border="1">
<tbody><tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<t开发者_开发百科r>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</tbody></table>
</div></div>
<p id="yui_3_3_0_1_1296942015298270" class="yui3-dd-drop">Proin et sem leo, sed luctus nisi. Suspendisse pharetra iaculis laoreet. Pellentesque vulputate; laoreet ultricies nunc facilisis adipiscing ultricies nunc.</p>
<div class="widget table">
<p>Table widget.</p>
<table width="80%" border="1">
<tbody><tr>
<td>1</td>
<td>
<ul>
<li>1</li>
<li>2<ul><li id="yui_2_0_0_1">nested</li></ul></li>
</ul>
</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td class="yui2-dd-drop yui3-dd-drop">7</td>
<td class="yui2-dd-drop yui3-dd-drop">8</td>
<td class="yui2-dd-drop yui3-dd-drop">9</td>
</tr>
</tbody></table>
</div>
</div>
Hopefully the above is all good, don't pick it apart too readily - as stated in comment below, its sample html.
You could try this monstrosity:
var dirty = 'class="yui3-dd-shim" class="safeClass yui3-dd-shim" class="yui3-dd-shim safeClass"';
var clean = dirty.replace(/class="yui[0-9]-[^\s]+"|\s?yui[0-9]-[^\s"]+\s?|id="yui_[0-9][^"]+"/gi, '');
Tested it on your sample data, seemed to do the job.
精彩评论