I am looking for a regex for Javascript to search for text ("span" for example) in HTML.
Example:
<div>Lorem span Ipsum dor<a href="blabla">lablala</a> dsad <span>2</span> ... </div>
BUT only the "span" after "Lorem" should be matched, not the <span> tag.
For a second example, if we search for "bla", only the bold text should be matched.
EDIT:
The HTML is gotten by innerHTML, the matchings will be surrounded with <span class="x">$text</span>, an then rewritten to innerHTML of this node, and all these without killing the other tags.
EDIT2 and My Solution:
I wrote my own search, it is searching char by char, with cache and flags.
Thanks for ure Help guys!
You could use dom methods to process every text node.
This method takes a parent node for the first argument and loops through all of its childnodes, processing the text nodes with the function passed as the second argument. The function is where you would operate on the test node’s data, to find or replace or delete or wrap the found text in a ‘highlighted’ span, for example.
You can call the function with only the first argument, and it will return an array of text nodes, and you can then use that array to manipulate the text- the array items in that case are each nodes, and have data, parents and siblings.
//test case
ucwords(document.body,/\bspan\b/ig)