简体   繁体   中英

Using regex to match part of a word or words

I'm new to regex and having difficulty with some basic stuff.

var name = "robert johnson";
var searchTerm = "robert johnson";

if (searchTerm.match(name)) {
    console.log("MATCH");
}

I'd like to try and find something that matches any of the following:

rob, robert, john, johnson, robertjohnson

To make the regex simpler, I've already added a .toLowerCase() to both the "name" and the "searchTerm" vars.

What regex needs to be added to searchTerm.match(name) to make this work?

Clarification: I'm not just trying to get a test to pass with the 5 examples I gave, I'm trying to come up with some regex where any of those tests will pass. So, for example:

searchTerm.match(name)

...needs to change to something like:

searchTerm.match("someRegexVoodooHere"+name+"someMoreRegexVoodooHere")

So, if I edit

var searchTerm = "robert johnson";

...to be

var searchTerm = "rob";

...the same function searchTerm.match directive would work.

Again, I'm new to regex so I hope I'm asking this clearly. Basically I need to write a function that takes any searchTerm (it's not included here, but elsewhere I'm requiring that at least 3 characters be entered) and can check to see if those 3 letters are found, in sequence, in a given string of "firstname lastname".

"robert johnson".match(/\b(john(son)?|rob(ert(johnson)?)?)\b/)

Will give you all possible matches (there are more then one, if you need to find whether the input string contained any of the words.

/\b(john(son)?|rob(ert(johnson)?)?)\b/.test("robert johnson")

will return true if the string has any matches. (better to use this inside a condition, because you don't need to find all the matches).

  • \\b - means word boundary.
  • () - capturing group.
  • ? - quantifier "one or none".
  • | - logical "or".

Regular expressions look for patterns to make a match. The answer to your question somewhat depends on what you are hoping to accomplish - That is, do you actually want matched groups or just to test for the existence of a pattern to execute other code.

To match the values in your string, you would need to use boolean OR matching with a | - using the i flag will cause a case insensitive match so you don't need to call toLowerCase() -

var regex = /(rob|robert|john|johnson|robertjohnson)/i;
regex.match(name);

If you want a more complex regex to match on all of these variations -

var names = "rob, robert, john, johnson, robertjohnson, paul";
var regex = /\b((rob(ert)?)?\s?(john(son)?)?)\b/i;
var matches = regex.match(names);

This will result in the matches array having 5 elements (each of the names except "paul"). Worth noting that this would match additional names as well, such as "rob johnson" and "rob john" which may not be desired.

You can also just test if your string contains any of those terms using test() -

var name = "rob johnson";
var regex = /\b((rob(ert)?)?\s?(john(son)?)?)\b/i;
if (regex.test(name)){
   alert('matches!');
}

You could create an array of the test terms and loop over that array. This method means less complicated regex to build in a dynamic environment

var name = "robert johnson";
var searchTerm = "robert johnson";

var tests = ['rob', 'robert', 'john', 'johnson', 'robertjohnson'];
var isMatch = false;

for (i = 0; i < tests.length; i++) {
    if (searchTerm.test(tests[i])) {
        isMatch = true;
    }
}

alert(isMatch)

/^\\s*(rob(ert)?|john(son)?|robert *johnson)\\s*$/i.test(str)

will return true if str matches either:

  • rob
  • robert
  • john
  • johnson
  • robertjohnson
  • robert johnson
  • robert johnson (spaces between these 2 does not matter)

and it just doesn't care if there are preceding or following empty characters. If you don't want that, delete \\s* from the beginning and the end of the pattern. Also, the space and asterisk between name and surname allows 0 or more spaces between those two. If you don't want it to contain any space, just get rid of that space and asterisk.

The caret ^ indicates beginning of string and the dollar sign $ indicates end of string. Finally, the i flag at the end makes it search case insensitively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM