는이 작업을 수행해야합니다
var txt = textDiv.replace(new RegExp(searchInput, 'gi'), function(str) {
return "<span class='highlight'>" + str + "</span>"
});
또는
var txt = textDiv.replace(
new RegExp(searchInput, 'gi'),
"<span class='highlight'>$&</span>");
gi
->
모든 대소 문자를 구분하지 일치하는 텍스트
$(document).ready(function() {
// globals
var searchInput;
var textDiv;
$('.searchBtn').click(function(event) {
event.preventDefault();
// set the values of the search and the text
searchInput = $('.searchInput').val();
textDiv = $('.textDiv').text();
var reg = new RegExp(searchInput, 'gi');
var txt = textDiv.replace(reg, function(str) {
return "<span class='highlight'>" + str + "</span>"
});
$('.textDiv').html(txt);
});
});
.textDiv {
height: 100px;
width: 500px;
text-align: center;
padding: 20px;
margin: 0 auto;
}
.highlight {
background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="main">
<div class="searchDiv">
<h3> Mask A Word In The Text: </h3>
<label>
<input class="searchInput" type="text" value = "iS"/> </label>
<button class="searchBtn" type="button"> Search </button>
</div>
<div class="textDiv">
Bastille Day is the common name given in English-speaking countries to the French National Day, which is celebrated on 14 July each year. In France, it is formally called La fête nationale (French pronunciation: [la fɛːt nasjɔnal]; The National Celebration) IS Is
and commonly Le quatorze juillet. (French pronunciation: [lə katɔʁz(ə) ʒɥijɛ]; the fourteenth of July).
</div>
</div>
</div>
(http://stackoverflow.com/help/on- 주제 : 디버깅 도움말을 찾는 질문 (" 왜이 코드가 작동하지 않습니까? ")에는 원하는 동작, 특정 문제 또는 오류 및 문제 자체 **를 재현하는 데 필요한 가장 짧은 코드가 포함되어야합니다. – JDB
분명히 당신은이 요구 사항을 극복하기 위해 jsfiddle 링크를 코드로 포맷 한 것을 고려하여 이것을 알고 있습니다. – JDB
(문자열, 단어) => string.replace (단어, (w) =>' $ {w}') – floribon