Hi to all,
I am new to java script.I want to highlight the search terms in text area.I want to do this by using regular expression.When eve i give the value to regular expression directly it works well.But if i give it frim variable it doesn't work well.My code is below.

function add(id) {                    
                    var i=0;
                    id = "#"+id.replace(/:/g, "\\:");
                    var s="fox";
                    var searchText='fox';          
                    var re = new RegExp(searchText,"g");
                    jQuery(id).caret(re);
                }

The above not works well. If i use /fox/g instead of re it works well.

<textarea id="Text" cols="30" rows="10">
The brown 'fox'121s is jumping 1 over the brown tree fox121s is a cruel animal
</textarea>
<input class="highlight3" id="searchID" type="button" value="highlight fox" onclick="add('form:Text')"/>

What is the reason for this?How can i achieve this.
Please help me.

Java != Java Script

Post moved

Let me see if I understand the problem.
Are you saying that this

jQuery(id).caret(/fox/g);

works as you expect but this

var searchText='fox';
var re = new RegExp(searchText,"g");
jQuery(id).caret(re);

does not?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.