You need use RegExp with u
tag but javascript won't support unicode regex :( so to solve this problem you should redefine \b
. \ba
means [^\w]a
so for turkish characters;
[^\wığüşöçĞÜŞÖÇİ]
is key to go.
[^\wığüşöçĞÜŞÖÇİ](türkçe)[^\wığüşöçĞÜŞÖÇİ]
can be used but this time it won't find any türkçe
in down below.
türkçe dili destekliyorum
to solve that problem you can add ^
and $
..
(?:^|[^\wığüşöçĞÜŞÖÇİ])(türkçe)(?:[^\wığüşöçĞÜŞÖÇİ]|$)
thats it..
Note: this regex will match previous character and next character. so you need to put them again. (^|[^\wığüşöçĞÜŞÖÇİ])(türkçe)([^\wığüşöçĞÜŞÖÇİ]|$)
and replace with $1<span class="match">$2</span>$3
.
Note: you can use look behind and look ahead too but unfortunately javascript doesn't support look behind
var word = 'İpsum';
var rgx = new RegExp('(^|[^\wığüşöçĞÜŞÖÇİ])(' + word + ')([^\wığüşöçĞÜŞÖÇİ]|$)', 'ig');
$('p, p *').contents().filter(function() {
return this.nodeType === 3;
}).each(function() {
$(this).replaceWith($(this).text().replace(rgx, "$1<span class='match'>$2</span>$3"));
});
var positions = $('.match').map(function() {
return this.getBoundingClientRect().top;
}).get();
div {
font-size: 50px;
}
span.match {
background: gold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<p>Lorem İpsum dolor sit amet, consectetur adipisicing elit. Aut voluptatum, provident saepe. Culpa animi sint, itaque iure error hic qui blanditiis perspiciatis adipisci, libero quia veritatis dignissimos quasi id cumque!</p>
</body>
Note: You can't search the special characters with this (like [hi] spe.cial characters
). You must use this