1

I'm trying to match a name obtained from server against input provided by a user, the complete name may or may not be known entirely by the user, for example I should be able to match the following:

Name Obtained: Manuel Barrios Pineda
Input given: Manuel P

Right until now I've tried with this code:

var name_obtained = 'Manuel Barrios Pineda';
var re = new RegExp('\b[' + name_obtained + ']+\b');
var input_string = 'Manuel';

if (input_string.match(re)) {
    alert('Match');
} else {
    alert('No Match');
}

Here's an example: jsfiddle example

EDIT 1: It's required to match input like 'Manuel B', 'Manuel P'

4
  • Double escape the b. new RegExp('\\b[' + name_obtained + ']+\\b'); String literal syntax has escape sequences as well, so to use a \b in the regex, you need to provide a literal \ character, which is done with \\ . Commented Jul 23, 2013 at 17:02
  • What specifically is the regex testing against? After fixing by double-escaping, or just removing them, it'll match "Manuel Pinzon", too. Commented Jul 23, 2013 at 17:03
  • if you can implement a full text search you can put string matching on your db instead in your application logic Commented Jul 23, 2013 at 17:04
  • Ok, I double-escaped that, it's "working" now, but as Dave Newton noticed input like 'Manuel Bartender' would match, any ideas how could the regex be more restrictive. @DaveNewton regex should match combinations like 'Manuel B', 'Manuel P'. Commented Jul 23, 2013 at 18:07

2 Answers 2

2
var name_obtained = 'Manuel Barrios Pineda';
var re = new RegExp('\b[' + name_obtained + ']+\b');

That's not working. Your building a character class to match a single character between word boundaries. The result will be equal to

var re = /\b[adeilnoruBMP]\b/;
input_string.match(name_obtained)

That would never work when the name_obtained is longer than the input_string. Notice that match will try to find the regex in the input_string, not the other way round.

Therefore I'd suggest to use something simple like

var name_obtained = 'Manuel Barrios Pineda';
var input_string = 'Manuel';

if (name_obtained.indexOf(input_string) > -1) {
    alert('Match');
} else {
    alert('No Match');
}

To use your input_string as a regex to search in the obtained name with omitting middle names or end characters, you could do

String.prototype.rescape = function(save) {
    return this.replace(/[{}()[\]\\.?*+^$|=!:~-]/g, "\\$&");
}

var name_obtained = 'Manuel Barrios Pineda';
var input_string = 'Manuel'; // or 'Manuel P', 'Manuel Pineda', 'Manuel B', 'Barrios P', …

var re = new RegExp("\\b"+input_string.rescape().split(/\s+/).join("\\b.+?\\b"));
if (re.test(name_obtained)) {
    alert('Match');
} else {
    alert('No Match');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Forgot to mention I should be able to match something like 'Manuel P', 'Manuel Pineda', 'Manuel B', etc
I tried your code, it won't match any input string, perhaps I missed something.
1

Perhaps you want something like this

var input_given = 'Manuel P'.replace(/[.+*()\[\]]/g, ' ').replace(/\?/g, '.'),
    name_obtained = 'Manuel Barrios Pineda',
    arr = input_given.split(' '), i, ret = true;

for (i = 0; i < arr.length && ret; ++i) // for each word A in input
    if (arr[i]) // skip ""
        ret = new RegExp('\\b' + arr[i]).test(name_obtained);
            // test for word starting A in name
// if test failed, end loop, else test next word

if (ret) alert('Match'); // all words from input in name
else alert('No Match');  // not all words found

3 Comments

Tried your solution and it is working like a charm, though I'm still trying to understand exactly how you achieved it as I'm a newbie when it comes to regular expressions.
The first line sanitises the input, it then tests each word in the input to see if the name contains words (that start, at least) with words from the input :)
The for loop is a little more complicated that normal, notice the && ret; i.e. it ends if ret becomes false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.