Skip to content

Instantly share code, notes, and snippets.

@furf
Last active August 29, 2015 14:00
Show Gist options
  • Save furf/11049377 to your computer and use it in GitHub Desktop.
Save furf/11049377 to your computer and use it in GitHub Desktop.
Fuzzy search and replace multiple terms in a string.
function replace(phrase, query) {
return phrase.replace( // SEARCH the phrase...
RegExp('(' + // using a REGULAR EXPRESSION to capture...
query // all matches in the QUERY.
.trim() // Trim whitespace to PREVENT "EMPTY" WORDS when the query is...
.split(/\s+/) // split into DISCREET SEARCHABLE "WORDS" (non-whitespace terms),...
.sort(function(a,b) { // SORTED by...
return b.length - a.length; // descending length to PREVENT LOSS OF MATCHES previously replaced by shorter matches.
}).map(function(word) { // To capture non-word characters like PERIODS, HYPHENS, and APOSTROPHES,
return word.split('') // BETWEEN EACH CHARACTER of the word
.join('\\W*'); // insert the regular expression for optional non-word characters.
}).join('|') // Capture matches of ANY
+ ')', 'gi'), // and ALL of our terms (CASE-INSENSITIVE)...
'<b>$1</b>'); // and REPLACE them with BOLD text.
}
[
'Don\'t you forget about me.',
'Leave those kids alone',
'Born in the U.S.A',
'Bombs over Baghdad',
'Us vs. them',
'don\'t leave me',
'u.s.a.! u.s.a.!'
].forEach(function(phrase) {
console.log(replace(phrase, ' dont leave us '));
});
var reWhitespace = /\s+/;
function replace(phrase, query) {
var terms = query.trim().split(reWhitespace).sort(sortByLengthDescending).map(insertNonWordChars);
var search = RegExp('(' + terms.join('|') + ')', 'gi');
var replace = '<b>$1</b>';
return phrase.replace(search, replace);
}
function sortByLengthDescending(a,b) {
return b.length - a.length;
}
function insertNonWordChars(word) {
return word.split('').join('\\W*');
}
[
'Don\'t you forget about me.',
'Leave those kids alone',
'Born in the U.S.A',
'Bombs over Baghdad',
'Us vs. them',
'don\'t leave me',
'u.s.a.! u.s.a.!'
].forEach(function(phrase) {
console.log(replace(phrase, ' dont leave us '));
});
var reWhitespace = /\s+/;
function replaceAll(items, query) {
var terms = query.trim().split(reWhitespace).sort(sortByLengthDescending).map(insertNonWordChars);
var search = RegExp('(' + terms.join('|') + ')', 'gi');
var replace = '<b>$1</b>';
$.each(items, function(title, $el) {
var match = title.replace(search, replace);
if (match.length > title.length) {
$el.text(match);
}
console.log($el.text());
});
}
function sortByLengthDescending(a,b) {
return b.length - a.length;
}
function insertNonWordChars(word) {
return word.split('').join('\\W*');
}
var els = {
'Don\'t you forget about me.': $('<div>').text('Don\'t you forget about me.'),
'Leave those kids alone': $('<div>').text('Leave those kids alone'),
'Born in the U.S.A': $('<div>').text('Born in the U.S.A'),
'Bombs over Baghdad': $('<div>').text('Bombs over Baghdad'),
'Us vs. them': $('<div>').text('Us vs. them'),
'don\'t leave me': $('<div>').text('don\'t leave me'),
'u.s.a.! u.s.a.!': $('<div>').text('u.s.a.! u.s.a.!')
};
replaceAll(els, ' dont leave us ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment