I need to compare two strings in a loop while accepting a certain character "-" within the matching string.
function onDatabound(e)
{
var grid = e.sender;
var str = $.trim(grid.value());
var srcWords = str.split(" ");
var records = $(grid.ul).find("li");
for (var i = 0; i < records.length; i++) {
var classes = $(records[i]).find("span i").attr("class");
var newText = records[i].innerText;
for (var j = 0; j < srcWords.length; j++) {
var regex = new RegExp(srcWords[j], 'gi'); //case-insensitive
newText = newText.replace(regex,
function(match) {
return '<strong>' + match + '</strong>';
});
}
$(records[i]).find("span").html("<i class='" + classes + "'></i>" + newText);
}
}
This code works perfectly but it needs the final touch. I need the replace-function to also accept inputs from users even if they don´t write the character "-". Example:
innerText: 1111-2222 lorem ipsum dolar 11112222 lorem ipsum dolar
User input: 11112222
I need both of these to be highlighted "1111-2222" and "11112222" even though the user wrote without the "-" or vice versa.
Should I use the regular expression to solve this or do you guys see any other method for me to use?