I am searching through text contained in multiple DIVS looking for an occurrence of a substring within a string.
Example: Searching for '1st', finding a match in the string (101st), and then i want to replace the entire string (101st) with another string (one hundred first).
I can locate the occurrence, but I can't figure out how to return the ENTIRE word the substring occurs in to replace it.
The scenario: The DIVS contain driving directions that contains street numbers. I am converting the numbered streets to words in order for the text to speech engine to correctly pronounce the numbers. So there may be a 91st which I would replace with ninety first, or a 21st that I would replace with twenty first. I have a function already written that will take care of any possible combinations of replacement words. I just need to figure out how to capture the entire string of the occurrence to implement the replace().
The reason I need the entire string (word) that the occurrence is in, is because the function numberToWords.toWordsOrdinal() I will pass the string to will take the entire string (101st) and convert it to one hundred first. I'm searching for all numbers ending in 0th, 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th and 9th and converting to the word version of the numbers. Any numbers that do not contain an ordinal will be left alone.
Here is the code I have:
$("div.dirInstruction").each(function() {
if ($(this).text().indexOf("1st") >= 0) {
// found occurrence
alert($(this).text()); // < ---- for visual debug - returns all text in div of match
var StringThatContainsMatch = ?????; <----- need whole word that contained match
var replaceNumber = numberToWords.toWordsOrdinal(StringThatContainsMatch);
var new_word = $(this).text().replace(StringThatContainsMatch, replaceNumber);
$(this).text(new_text);
}
});