Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace every word with tag

JAVASCRIPT or JAVA solution needed

The solution I am looking for could use java or javascript. I have the html code in a string so I could manipulate it before using it with java or afterwards with javascript.

problem

Anyway, I have to wrap each word with a tag. For example:

<html> ... >
Hello every one, cheers
< ... </html>

should be changed to

<html> ... >
<word>Hello</word> <word>every</word> <word>one</word>, <word>cheers</word>
< ... </html>

Why?

This will help me use javascript to select/highlight a word. It seems the only way to do it is to use the function highlightElementAtPoint which I added in the JAVASCRIPT hint: It simply finds the element of a certain x,y coordinate and highlights it. I figured that if every word is an element, it will be doable.

The idea is to use this approach to allow us to detect highlighted text in an android WebView even if that would mean to use a twisted highlighting method. Think a bit more and you will find many other applications for this.


JAVASCRIPT hint

I am using the following code to highlight a word; however, this will highlight the whole text belonging to a certain tag. When each word is a tag, this will work to some extent. If there is a substitute that will allow me to highlight a word at a certain position, it would also be a solution.

function highlightElementAtPoint(xOrdinate, yOrdinate) {
    var theElement = document.elementFromPoint(xOrdinate, yOrdinate);
    selectedElement = theElement;
    theElement.style.backgroundColor = "yellow";
    var theName = theElement.nodeName;
    var theArray = document.getElementsByTagName(theName);
    var theIndex = -1;
    for (i = 0; i < theArray.length; i++) {
        if (theArray[i] == theElement) {
            theIndex = i;
        }
    }
    window.androidselection.selected(theElement.innerHTML);
    return theName + " " + theIndex;
}
like image 674
Sherif elKhatib Avatar asked Dec 09 '22 23:12

Sherif elKhatib


1 Answers

Try to use something like

String yourStringHere = yourStringHere.replace(" ", "</word> <word>" )
yourStringHere.replace("<html></word>", "<html>" );//remove first closing word-tag

Should work, maybe u have to change sth...

like image 92
Thkru Avatar answered Dec 11 '22 11:12

Thkru