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

老子叫甜甜 提交于 2019-12-25 03:45:15

问题


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*BEARING* 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.


回答1:


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/



来源:https://stackoverflow.com/questions/6825700/highlight-all-matching-strings-substrings-on-a-datagrid-from-a-keyword-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!