Question: ```
<div>John Resig</div>
<div>George Martin</div>
<div>Malcom John Sinclair</div>
<div>J. Ohn</div>
<script>
$("div:contains('John')").css("text-decoration", "underline");
</script>

```

The above code will add underline to the total DIV which contains "John".But how can i make only "John" to be underlined.  

Please someone help me.

Answer: 
You'll have to wrap your text in a span:

```
$("div:contains('John')").each(function()
{
    var $el = $(this);
    $el.html( $el.html().replace(/John/g, '<span style="text-decoration: underline">John</span>') );
});

```

<http://jsfiddle.net/GP96T/2/>

As noted by @Brad Christie, you should only use this if you either know that you don't have "John" somewhere in your attributes, or the `div`s have no children.
Hackish way:

```
<div>John Resig</div>
<div>George Martin</div>
<div>Malcom John Sinclair</div>
<div>J. Ohn</div>
<script>
    $("div:contains('John')").each(function(i,e){
        var $this = $(this);
        var html = $this.html()
            .replace(/John/g,'<span style="text-decoration:underline;">John</span>');
        $this.html(html);
    });
</script>

```

As Seen [here](http://jsfiddle.net/bradchristie/h6uVJ/)

But be careful, if you have "John" in a link, or a title tag, this will also replace those instances as well. e.g.

```
<div><img src="..." title="This is John" /></div>

```

becomes:

```
<div><img src="..." title="This is <span style="text-decoration:underline;">John</span>" /></div>

```

*(undesirable I would imagine)*