Get parentNode of text with javascript regex
I have RegEX:
var re = RegExp("(?:^\\W*|(" + motBefore.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + ")\\W+)" + motErreur.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "(?:\\W+(" + motAfter.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + ")|\\W*$)", "g"); With the help of this RegEX, I can find on my page the place of a certain word regarding positioning (for example, I do not have multiple occurrences if this is not the one I want).
I would like to combine motErreur with span. The fact is, I cannot do this with a simple replacement and change of innerHTML, because it will ruin the tags of my page.
I use:
var result = document.getElementById('edth_corps'); var textRangeGlobal = document.body.createTextRange(); textRangeGlobal.moveToElementText(result); if(textRangeGlobal.findText(motErreur)){ var html= "<span id='"+nbId+"' class='erreurOrthographe' oncontextmenu='rightClickMustWork(event, this);return false'>" + motErreur + "</span>"; textRangeGlobal.pasteHTML(html); } I would like var result not to be document.getElementById('edth_corps'); , and Node, where I got the result using my Regex.
I'm on IE5 and can't figure out how to get Node using RegEx (I tried with match , but it didn't work.
Does anyone have an idea how to do this?
Using regex is not a good approach to this problem. You would be better off creating a dom element and using the dom traversal to remove unwanted elements. You can make this code similar to:
var wrap = document.createElement('div'); wrap.innerHTML = "your html"; Now you can work with the "virtual" element using dom selectors. and add node to dom
target.appendChild(wrap.firstElementChild.cloneNode(true);