Last active
August 29, 2015 14:00
-
-
Save furf/11049377 to your computer and use it in GitHub Desktop.
Fuzzy search and replace multiple terms in a string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ')); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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