0

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.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by fxm
0

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.