[英]How to highlight a part of string from given string in Javascript/Jquery
I have a string 我有一串
var inputStr = 'xzy';
var match = 'x';
I have to highlight the str
from the inputStr
. 我必须从
inputStr
突出显示str
。 I was trying something like this 我正在尝试这样的事情
var inputStr = "vikram";
var match = 'v';
var new_str = str.split('');
for(var i = 0;i<=new_str.length;i++)
{
if(match == new_str[i])
{
var x_str += ''//code to bold that charachter and add in the array
}
}
Can any one please help me fix this 谁能帮我解决这个问题
Thanks in advance 提前致谢
我建议乍一看:
var inputStr = inputStr.replace(new RegExp('(' + str + ')', 'g'), '<strong>$1</strong>');
For your question i found that you want to implement a search similar to that of the browser check out the Fiddle that i have created hope that helpes you. 对于您的问题,我发现您想实现与浏览器类似的搜索,请查看我创建的Fiddle,希望对您有所帮助。
[http://jsfiddle.net/raLnLyb4/][1]
var $search = 'vi';
var $names = $('.search_string p');
var match = RegExp($search, 'gi');
$names.filter(function(){ return match.test($(this).text()) })
.html(function(){
if (!$search) return $(this).text();
return $(this).text().replace(match, '<span class="highlight">$&</span>');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.