Regular expressions, why so difficult?

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.

4 Likes

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.

1 Like

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:

  1. First Regular Expression:
let myRegex = /(Franklin|Eleanor) (([A-Z]\.?|[A-Z][a-z]+) )?Roosevelt/;

  1. This regular expression is designed to match names in the format “Franklin Roosevelt” or “Eleanor Roosevelt.” Here’s a breakdown:
  • (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.”
  1. Second Regular Expression:
let myRegex = /(Eleanor|Franklin) .*Roosevelt/;

  1. This second regular expression is a simplified version of the first one. It matches names in the format “Eleanor Roosevelt” or “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:

  • Both regular expressions are designed to be used with the test method on a string (myString). This method returns true if the string matches the pattern and false otherwise.
  • The first regular expression is more flexible, allowing for variations in the first name and capturing it as an optional group.
  • The second regular expression is more straightforward and assumes a direct match between the specified first names and the last name “Roosevelt” with any characters in between.

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).