0

I have a JSP where in a text area i am reading the String array of a whole sentence and printing it line by line. No i want to highlight some patter in the line in BOLD and red color font. e.g. if the line which is being read contains pattern "error" (it can be a single word or a part of a word like "initializationerror" or ""), i t will be highlighted in BOLD and red color. The rest of the line will be print as it is.

Below is the code snippet:

<table border="2">
<% 
    if(session.getAttribute("Result") != null)
    {
        String Result = (String) session.getAttribute("Result");
        if(Result.length() != 0)
        {
            String[] split_EOL = Result.split("\n");
%>
            <tr align="center"> 
                <td>
                    // Text area start here 
                    <textarea rows="50" cols="100" readonly="yes">   
<%
                        for(int i = 0; i < split_EOL.length; i++)
                        {
                            out.println(split_EOL[i]);
                            out.println(" ");
                        }
%>
                    </textarea>
                 </td>
             </tr>
<%
         }
    }
%>
</table>

Please help

I tried the following:

<textarea rows="50" cols="100" readonly="yes"> 
<% 
     for(int i=0;i<split_EOL.length ; i++){ 
     out.println(split_EOL[i].replaceAll("\\b\\w*"+Pattern+"\\w*\\b", "<b>$0</b>"));   
     out.println(" "); 

                                          }
%> 
</textarea> 

here Pattern = ERROR..... but it displays like this

95323:[<b>ERROR</b>] Logger not set 

even i trued to display some word in bold in out.println but it didnt render the bold tags:

 out.println("<b>"+Pattern+"</b>");%>

displays only

<b>ERROR</b>
user2014600
  • 13
  • 2
  • 7
  • Have you written `Pattern = ERROR` or is it `Pattern = "ERROR"`? – Olaf Dietsche Jan 27 '13 at 16:39
  • I completely missed your `textarea`, sorry. This is a multi line text input entry, which doesn't allow text attributes. So my solution is only valid for text outside your textarea. – Olaf Dietsche Jan 27 '13 at 16:45
  • Look at this answer [How can I display bold text in a textarea?](http://stackoverflow.com/a/9274621/1741542), maybe this is what you need. – Olaf Dietsche Jan 27 '13 at 16:49
  • So is there anyhting else i can use instead of textarea where i can display bold text.....My text area is read only so i dont need the user to edit...ther reason i used textarea was i have lots of lines to dislay so i can easily make it scrollabble – user2014600 Jan 27 '13 at 17:24
  • Hey @Olaf Dietsche Thanks a lot for the help. I got a work around and used div tags instead of text area..all thanks to you make me realize in time that text area cant render html. I have one more question regarding regex in the code. How do you make it ignore case sensitivity? Thanks again for the help – user2014600 Jan 27 '13 at 23:18

3 Answers3

1

You can replace the pattern \b\w*error\w*\b with a <span class="error-string">$0</span>

out.println(split_EOL[i].replaceAll("\\b\\w*error\\w*\\b", "<span class=\"error-string\">$0</span>"));

and define in CSS

error-string {
    color: red;
    font-weight: bold;
}

If you need a case insensitive search, you can prefix your pattern with (?i), see Pattern - Special constructs.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Bt how do i identify whether that pattern is present in the line....shall i brek the whole line ito tokens and match tokens with the word? I am reading one full line from the array in every iteration. And is there a way without css because i ahevnt use css in my jsp – user2014600 Jan 27 '13 at 00:02
  • @user2014600 You can use an inline style as Ali Bassam suggested, but I wouldn't recommend it. Using classes and CSS is cleaner than that. – Olaf Dietsche Jan 27 '13 at 00:08
  • I tried to use the following but its not rendering the html tags: here Pattern = ERROR..... but it displays like this 95323:[ERROR] Logger not set – user2014600 Jan 27 '13 at 15:51
  • View source display like this <b>ERROR</b> – user2014600 Jan 27 '13 at 16:11
  • @user2014600 Please add the updated code snippet to your question, it is easier to read there. If your replacement string is `$0` the output seems correct. What is your expected output? – Olaf Dietsche Jan 27 '13 at 16:17
  • Infact the truth is none of the html tags are getting rendered in the text area....... I tried <%=Pattern%> but it didnt worked – user2014600 Jan 27 '13 at 16:55
  • @user2014600 Please see my comments to your question. – Olaf Dietsche Jan 27 '13 at 17:02
0
<%if( //your condition )
  {
    out.println("<span style=\"font-weight:bold; color:red; \"> Bla Bla Bla </span>");
  }
%>
Ali Bassam
  • 9,691
  • 23
  • 67
  • 117
0

the easy way is: string textString="this is my text i would like to highlight the word text";

string highlight= "<mark>text</mark>"; ;

<%=textString.replaceAll("text", highlight) %>

eran
  • 1