There's only one reliable way of doing this: Stepping through the DOM and searching for a match in each text node.
This function will do just that:
function findTextNodeWithMatch(regex, context) {
context = context || document.documentElement;
var childNodes = context.childNodes,
retTextNodes = [],
i = -1, node;
while ( node = childNodes[++i] ) {
if (node.nodeType === 3) {
var m;
while (m = regex.exec(node.data)) {
retTextNodes.push({
node: node,
match: m
});
}
}
if (node.nodeType === 1 && node.nodeName.toLowerCase() !== 'script') {
retTextNodes = retTextNodes.concat( arguments.callee( regex, node ) );
}
}
return retTextNodes;
}
Usage:
var patternToMatch = /\[((?:\\\[|.)*)\]/g;
var matchingNodes = findTextNodeWithMatch( patternToMatch, $('.comment-body p')[0] );
for (var i = 0, len = matchingNodes.length; i < len; i++) {
// Loop through the matching nodes:
// Extract the string you want:
var theMatch = matchingNodes[i].match;
var myString = theMatch[1];
alert( myString );
// Replace the string within the node:
var theNode = matchingNodes[i].node;
theNode.parentNode.replaceChild( document.createTextNode(theNode.data.replace(patternToMatch, '')), theNode );
}
Using an element's innerHTML
is not reliable since it may contain other elements and those elements may have attributes that match what you're looking for - you don't want to remove them by mistake...