1

I have the following html in a page:

<p>foo
<br>bar
<!--additional br elements of variable number-->
<br>https://example.com
<!--additional br elements of variable number-->
<br>baz
</p>

The link appears in plain text and I need to linkify it.

This does NOT yield any match:

document.evaluate( '//*[contains(text(),"https")]' ,document, 
    null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;

If I use '//text()[contains(.,"https")]' I get the entire text but not a reference to a DOM element that I can then modify (for adding an <a> element around it so that is becomes clickable.)

How can I select the element containing this plain text URL in a way that I can then modify it?

GJ.
  • 5,226
  • 13
  • 59
  • 82

2 Answers2

0
/p/text()[3]

This is my first answer.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 5
    Welcome to Stack Overflow. I tried editing this to remove 'this is my first answer', since it isn't relevant, but the resulting answer is too short to be accepted. Would you edit, perhaps adding some more detail about why you'd do it this way? – halfer Mar 21 '13 at 17:00
0

It seems that '//p/text()[contains(.,"https")]/..' will work for selecting the p element, and then a regex search and replace on the p's innerHTML would complete the process.

GJ.
  • 5,226
  • 13
  • 59
  • 82