Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match partial words (JavaScript)

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.

like image 562
nw. Avatar asked Apr 16 '10 17:04

nw.


People also ask

How to match text in js?

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.

What does match() return JavaScript?

The match() method returns an array with the matches. The match() method returns null if no match is found.

What does the match() method return?

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.

What does the regular expression '[ a za z ]' match?

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.


1 Answers

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:

  • matches[0] => the entire expression (useless)
  • matches[odd] => one of our matches
  • matches[even] => additional text not on the original match string

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];
}
like image 143
Fábio Batista Avatar answered Nov 14 '22 21:11

Fábio Batista