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”.
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.
will return
trueif 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”.