Replace each word with a tag

Required JAVASCRIPT or JAVA Solution

The solution I'm looking for can use java or javascript. I have html code in a string, so I can manipulate it before using it with java or subsequently with javascript.

Problem

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

<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 the word. It seems the only way to do this is to use the highlightElementAtPoint function, which I added in the JAVASCRIPT tooltip: it just finds an element of a specific x, y coordinate and selects it. I realized that if every word is an element, it will be doable.

The idea is to use this approach so that we can detect the selected text in the Android web interface, even if it would mean using the twisted highlight method. Think a little more and you will find many other applications for this.


JAVASCRIPT Tip

I use the following code to highlight a word; however, this will highlight all the text belonging to a particular tag. When every word is a tag, this will work to some extent. If there is a replacement that will allow me to highlight a word in a certain position, this will 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; } 
+4
source share
4 answers

Try using something like

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

Should work, maybe you need to change sth ...

+6
source
 var tags = document.body.innerText.match(/\w+/g); for(var i=0;i<tags.length;i++){ tags[i] = '<word>' + tags[i] + '</word>'; } 

Or as @ThomasK said:

 var tags = document.body.innerText; tags = '<word>' + tags + '</word>'; tags = tags.replace(/\s/g,'</word><word>'); 

But you should keep in mind: .replace(" ",foo) replaces space only once. For multiple replacements, you should use .replace(/\s+/g,foo)

And as @ ajax333221 said, the second way would include commas, periods, and other characters, so the best solution is the first

JSFiddle example: http://jsfiddle.net/c6ftq/4/

+4
source
 inputStr = inputStr.replaceAll("(?<!</?)\\w++(?!\\s*>)","<word>$0</word>"); 
+2
source

You can try the following code,

 import java.util.StringTokenizer; public class myTag { static String startWordTag = "<Word>"; static String endWordTag = "</Word>"; static String space = " "; static String myText = "Hello how are you "; public static void main ( String args[] ) { StringTokenizer st = new StringTokenizer (myText," "); StringBuffer sb = new StringBuffer(); while ( st.hasMoreTokens() ) { sb.append(startWordTag); sb.append(st.nextToken()); sb.append(endWordTag); sb.append(space); } System.out.println ( "Result:" + sb.toString() ); } } 
+2
source

All Articles