I'm making a script that highlights a part of a string by wrapping it in a span. (the part that is highlighted is decided via a form)
Right now I've got this.
value.replace(term,'<span>'+term+'</span>')
$(document).ready(function(){
var term = $('.n').val();
var matches = ['Tree of life','Life of the tree'];
$('.output>div').remove();
$.each(matches,function(index,value){
$('.output').append('<div>'+value.replace(term,'<b>'+term+'</b>')+'</div>')
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<input type="text" class="n" value="Tree">
<h4>Output</h4>
<div class="output">
</div>
But thing is, this needs the term to be the same capitalization.
So if I'm searching for Tree:
- Tree of life -> has a match
- Life of the tree -> doesn't have a match
So how can I do a replace in JS, but that doesn't take in account the capitalization?