I would like to craft a case-insensitive regex (for JavaScript) that matches street names, even if each word has been abbreviated. For example:
n univ av should match N University Ave
king blv should match Martin Luther King Jr. Blvd
ne 9th should match both NE 9th St and 9th St NE
Bonus points (JK) for a "replace" regex that wraps the matched text with <b>
tags.
The string. match() is an inbuilt function in JavaScript used to search a string for a match against any regular expression. If the match is found, then this will return the match as an array. Parameters: Here the parameter is “regExp” (i.e. regular expression) which will compare with the given string.
The match() method returns an array with the matches. The match() method returns null if no match is found.
The MATCH function searches for a specified item in a range of cells, and then returns the relative position of that item in the range. For example, if the range A1:A3 contains the values 5, 25, and 38, then the formula =MATCH(25,A1:A3,0) returns the number 2, because 25 is the second item in the range.
For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.
You got:
"n univ av"
You want:
"\bn.*\buniv.*\bav.*"
So you do:
var regex = new RegExp("n univ av".replace(/(\S+)/g, function(s) { return "\\b" + s + ".*" }).replace(/\s+/g, ''), "gi");
Voilà!
But I'm not done, I want my bonus points. So we change the pattern to:
var regex = new RegExp("n univ av".replace(/(\S+)/g, function(s) { return "\\b(" + s + ")(.*)" }).replace(/\s+/g, ''), "gi");
And then:
var matches = regex.exec("N University Ave");
Now we got:
So, we can write:
var result = '';
for (var i=1; i < matches.length; i++)
{
if (i % 2 == 1)
result += '<b>' + matches[i] + '</b>';
else
result += matches[i];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With