Skip to content

Instantly share code, notes, and snippets.

@meglio
Created September 13, 2012 18:37
Show Gist options
  • Save meglio/3716561 to your computer and use it in GitHub Desktop.
Save meglio/3716561 to your computer and use it in GitHub Desktop.
Highlights numerical string in numerical string, while both strings are free formatted and containing more then just digits
function highlightNumerical(searchString, inString) {
// http://jsfiddle.net/meglio/p9KUV/
if (typeof searchString == "object") {
if (searchString.length)
searchString = searchString[0]
else
return inString
}
var prefix = '<strong class="highlight">', suffix = '</strong>',
normSearch = $.trim(searchString.replace(/\D/g, ''))
if (normSearch == '')
return inString
var expr = '('+ normSearch.split('').join('\\D*') + ')',
r = new RegExp(expr, 'g');
return inString.replace(r, prefix+'$1'+suffix);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment