$(document).ready(function() {
// Summary: Removes an element from the DOM by substring matching attribute selectors.
// Params: att, beginsWith, endsWith, bContains, bCaseInsensitive, bBeginsAndEndsWith
function removeByAttSubstring(att, beginsWith, endsWith, bContains, bCaseInsensitive, bBeginsAndEndsWith) {
// Assign string variables for each selector that we want to create
var sBw = att + "^=" + beginsWith,
sEw = att + "$=" + endsWith,
sCbw = att + "*=" + beginsWith,
sCew = att + "*=" + endsWith;
// Create an array of the string selectors
var aSelectors = [sBw, sEw, sCbw, sCew];
// If boolean case insensitive equals true, add those strings as well
if (bCaseInsensitive === true) {
var sBwCi = att + "^=" + beginsWith + " i",
sEwCi = att + "$=" + endsWith + " i",
sCbwCi = att + "*=" + beginsWith + " i",
sCewCi = att + "*=" + endsWith + " i";
aSelectors.push(sBwCi, sEwCi, sCbwCi, sCewCi);
}
// If boolean stack attributes equals true, combine the strings
if (bBeginsAndEndsWith === true) {
var sBwaew = sBw + "][" + sEw;
aSelectors.push(sBwaew);
}
// If booleans case insensitive and stack attributes equals true, combine the case insensitive strings
if (bCaseInsensitive === true && bBeginsAndEndsWith === true) {
var sBwaewCi = sBw + " i][" + sEw + " i";
aSelectors.push(sBwaewCi);
}
// For each string in the array, construct the attribute selector.
for (var i = 0; i < aSelectors.length; i++) {
aSelectors[i] = "[" + aSelectors[i] + "]";
}
// Format the jQuery Multiple Selector
var sSelectors = aSelectors.join(", ");
// Illustration purposes only
console.log("Attribute Selectors: " + sSelectors);
// Remove the elements, if matched, entirely from the DOM
$(sSelectors).remove();
}
// Initialize function
removeByAttSubstring("id", "yui_", "_32", true, true, true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="sm-eu-cookie-message" id="yui_3_8_0_1_1478749888956_changedfordemo">
Id attribute starts with <em>yui_</em>
</div>
<div class="sm-eu-cookie-message" id="changedfordemo_3_8_0_1_1478749888956_32">
Id attribute ends with <em>_32</em>
</div>
<div class="sm-eu-cookie-message" id="yUI_3_8_0_1_1478749888956_changedfordemo">
Id attribute starts with <em>yUI_</em>
</div>
<div class="sm-eu-cookie-message" id="changedfordemo_3_8_0_1_1478749888956_32">
Id attribute containing an instance of <em>_32</em>
</div>
<div class="sm-eu-cookie-message" id="changedfordemo_3_8_0_yui_1478749888956_changedfordemo">
Id attribute containing an instance of <em>yui_</em>
</div>
<div class="sm-eu-cookie-message" id="changedfordemo_3_8_0_yUI_1478749888956_changedfordemo">
Id attribute containing an instance of <em>yUI_</em>
</div>