-5

Can anybody help me decoding this RegExp?

/^(.+)\s{1}\((.*)\)$/
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
John Alba
  • 265
  • 2
  • 4
  • 14

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,721
  • 1
  • 25
  • 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
  • 523
  • 2
  • 11
Ekkstein
  • 771
  • 11
  • 20