et myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor) (([A-Z]\.?|[A-Z][a-z]+) )?Roosevelt/;
let result = myRegex.test(myString);
или
let myRegex = /(Eleanor|Franklin) .*Roosevelt/;
что тут можно понять?
и как это работает
et myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor) (([A-Z]\.?|[A-Z][a-z]+) )?Roosevelt/;
let result = myRegex.test(myString);
или
let myRegex = /(Eleanor|Franklin) .*Roosevelt/;
что тут можно понять?
и как это работает
Many people fine regular expressions difficult. They are quite a powerful tool and can be both very simple and extremely complex depending on what you’re trying to do.
Using a tool like Regex101 will help considerably when working with regular expressions.
A summary of the various parts of the regex are:
(Eleanor|Franklin)
matches either of those two words.
[A-Z]\.?
matches a single capital letter A through Z, optionally followed by a period.
[A-Z][a-z]+
matches a single capital letter A through z, followed by one or more lower case letters a through z.
(...)?
the ? means the previous item (the expression in the parenthesis here) is optional
.*
matches any character any number of times (including 0 times).
If you put them into Regex101 it will give you a more detailed description.
Regular expressions are about half a century old. Wikipedia suggests they originated from formats for mathematical formula. The highly cryptic syntax of regular expressions are more easily understood by computers and half a century ago computers were tremendously less powerful.
Understanding Regular Expressions can indeed be challenging for many individuals, but breaking down the given regular expressions can make it more accessible. Let’s dissect the provided regular expressions and explain their functionality:
let myRegex = /(Franklin|Eleanor) (([A-Z]\.?|[A-Z][a-z]+) )?Roosevelt/;
(Franklin|Eleanor)
: This part matches either “Franklin” or “Eleanor.”(([A-Z]\.?|[A-Z][a-z]+) )?
: This part is a bit more complex:
([A-Z]\.?|[A-Z][a-z]+)
: This section matches an initial uppercase letter followed by an optional period (.
) or an uppercase letter followed by lowercase letters. This corresponds to first names like “F.,” “Frank,” or “Eleanor.”( )?
: The entire preceding pattern is wrapped in parentheses with a question mark, making it optional. This allows for matching both full names (with a first name) or just the last name “Roosevelt.”Roosevelt
: This part simply matches the last name “Roosevelt.”let myRegex = /(Eleanor|Franklin) .*Roosevelt/;
(Eleanor|Franklin)
: Similar to the first regex, this part matches either “Eleanor” or “Franklin.”.*
: This part matches any characters (except for a newline) zero or more times. It represents the space between the first and last names.Roosevelt
: Like before, this part matches the last name “Roosevelt.”How it works:
test
method on a string (myString
). This method returns true
if the string matches the pattern and false
otherwise.These regular expressions are used to check if a given string (presumably a person’s name) matches the specified patterns, providing flexibility for different formats while capturing the essential components (first name and last name).