开发者

Highlight all matching strings/substrings on a datagrid from a keyword search

开发者 https://www.devze.com 2023-03-22 17:41 出处:网络
Can someone help me on how to highlight the matching strings/substrings from a keyword search? For example if the user inputs \"BEARING\", the datagrid should display the following

Can someone help me on how to highlight the matching strings/substrings from a keyword search?

For example if the user inputs "BEARING", the datagrid should display the following

ADAPTER BEARING

BAR AIR*BEARI开发者_Go百科NG* TURN

BEARING BALL

BEARING BRONZE

I am almost finish but in this example, the whole AIRBEARING is highlighted which supposed to be, it is only the BEARING that should be highlighted only.


Create a css class and call it 'highlight':

.highlight { background-color: yellow; }

Then use a regex replace to wrap that text with the class:

function highlight(walloftext, valuetohighlight) {
    var x = new RegExp("(" + valuetohighlight + ")", "gi");
    return walloftext.replace(x, '<span class="highlight">$1</span>');
}

http://jsfiddle.net/rkw79/5cCuc/

0

精彩评论

暂无评论...
验证码 换一张
取 消