-5

Can anybody help me decoding this RegExp?

/^(.+)\s{1}\((.*)\)$/
Sergio Tulentsev
  • 210,238
  • 40
  • 347
  • 343
John Alba
  • 195
  • 1
  • 3
  • 12

2 Answers2

1

// is simply placeholder for regexp, ^(.+)\s{1}\((.*)\)$ remains

^ means start of the string, $ is end of the string, (.+)\s{1}\((.*)\) remains

(.+) is first group of items, it matches any chars (if there's no chars the + will fail, because it means "give me 1 or more characters"), \s{1}\((.*)\) still remains

\s{1} means "give me any exactly one whitespace", \((.*)\) still remains

\( and \) means that you are encoded brackets to use their literal form, because simply using () is as a match group, (.*) left

(.*) is the same as (.+), but here also zero characters will match, since * means "give me anything, even nothing"

eg. Patryk (patnowak) will pass and Pat Nowak won't

PatNowak
  • 5,255
  • 1
  • 20
  • 31
0

/^(.+)\s{1}((.))$/
Would match something like: Hello (1id93;) To go step by step, since this is what a regex does:
/ - opens the regex
^ - matches at the start
( - opens a matching group, that will be returned in: $1
.+ - matches any character one or more times
) - closes the matching group
\s - matches an empty space
{1} - exactly one of them
( - matches a ( the backslash makes sure it matches the ( character (.
) - again matches anything 0 or several times and returns matching group $2
) - matches a ) $ - marks the end of the regex. / - closes the regex

haggisandchips
  • 501
  • 2
  • 11
Ekkstein
  • 662
  • 10
  • 19