I am building an javascript application for which I need to know the html tags that belong to an user selection and then for easy use put them in an array.
I used htmlText
which gave me a string that looks something like this:
<h1><span style="color: rgb(102, 51, 153); font-weight: bold; text-decoration: underline;"><sub>test</sub></span></h1>
Since I have hardly any knowledge of regular expressions and what I know just doesn't seem to do what I want, I was hoping one of you guys could help me on this part.
So what is the best way to make the above string look like the following array?
<h1>,
<span style="color: rgb(102, 51, 153); font-weight: bold; text-decoration: underline;">,
<sub>
My code so far (Don't know if I am on the right track though):
var fullhtml = SEOM_common.range.htmlText;//Get user selection + Surrounding html tags
var tags = fullhtml.split(SEOM_common.selected_value);//Split by user selection
var tags_arr = tags[0].match(/<(.+)>/);//Create array of tags
Thanks guys for the answers and comments. I managed to build the following method, which does exactly what I want.
find_all_parents : function(selectRange,endNode){
var nodes = [];
var nodes_to_go = [];
if(selectRange.commonAncestorContainer) nodes_to_go.push(selectRange.commonAncestorContainer.parentNode);//all browsers
else nodes_to_go.push(selectRange.parentElement());//IE<9 browsers
var node;
while( (node=nodes_to_go.pop()) && node.tagName.toLowerCase() != endNode){
if(node.nodeType === 1){ //only element nodes (tags)
nodes.push(node);
}
nodes_to_go.push(node.parentNode);
}
return nodes;
}