Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems like you aren't really familiar with what your regex is doing currently, which would be a good first step before modifying it. Let's walk through your regex using the email address <code>john.robert.smith@mail.com</code> (in each section below, the bolded part is what is matched by that section):</p> <ol> <li><p><code>^</code> is the <a href="http://www.regular-expressions.info/anchors.html">start of string anchor</a>. It specifies that any match must begin at the beginning of the string. If the pattern is not anchored, the regex engine can match a substring, which is often undesired.</p> <p>Anchors are zero-width, meaning that they do not capture any characters.</p></li> <li><p><code>[_a-z0-9-]+</code> is made up of two elements, a <a href="http://www.regular-expressions.info/charclass.html">character class</a> and a <a href="http://www.regular-expressions.info/repeat.html">repetition modifer</a>:</p> <ul> <li><code>[...]</code> defines a character class, which tells the regex engine, <em>any of these characters are valid matches</em>. In this case the class contains the characters a-z, numbers 0-9 and the dash and underscore (in general, a dash in a character class defines a range, so you can use <code>a-z</code> instead of <code>abcdefghijklmnopqrstuvwxyz</code>; when given as the last character in the class, it acts as a literal dash).</li> <li><code>+</code> is a repetition modifier that specifies that the preceding token (in this case, the character class) can be repeated one or more times. There are two other repetition operators: <code>*</code> matches zero or more times; <code>?</code> matches exactly zero or one times (ie. makes something <em>optional</em>).</li> </ul> <p>(captures <strong>john</strong>.robert.smith@mail.com)</p></li> <li><p><code>(\.[_a-z0-9-]+)*</code> again contains a repeated character class. It also contains a <a href="http://www.regular-expressions.info/brackets.html">group</a>, and an escaped character:</p> <ul> <li><code>(...)</code> defines a group, which allows you to group multiple tokens together (in this case, the group will be repeated as a whole).<br>Let's say we wanted to match 'abc', zero or more times (ie. abcabcabc matches, abcccc doesn't). If we tried to use the pattern <code>abc*</code>, the repetition modifier would only apply to the <code>c</code>, because c is the last token before the modifier. In order to get around this, we can group abc (<code>(abc)*</code>), in which case the modifier would apply to the entire group, as if it was a single token.</li> <li><code>\.</code> specifies a literal dot character. The reason this is needed is because <code>.</code> is a special character in regex, meaning <a href="http://www.regular-expressions.info/dot.html">any character</a>. Since we want to match an actual dot character, we need to escape it.</li> </ul> <p>(captures john<b>.robert.smith</b>@mail.com)</p></li> <li><p><code>@</code> is not a special character in regex, so, like all other non-special characters, it matches literally. <br>(captures john.robert.smith<b>@</b>mail.com)</p></li> <li><p><code>[a-z0-9-]+</code> again defines a repeated character class, like item #2 above. <br>(captures john.robert.smith@<b>mail</b>.com)</p></li> <li><p><code>(\.[a-z0-9-]+)*</code> is almost exactly the same pattern as #3 above. <br>(captures john.robert.smith@mail<b>.com</b>)</p></li> <li><p><code>$</code> is the end of string anchor. It works the same as <code>^</code> above, except matches the end of the string.</p></li> </ol> <hr> <p>With that in mind, it should be a bit clearer how to add a section with captures a plus segment. As we saw above, <code>+</code> is a special character so it has to be escaped. Then, since the + has to be followed by some characters, we can define a character class with the characters we want to match and define its repetition. Finally, we should make the whole group optional because email addresses don't need to have a + segment:</p> <pre><code>(\+[a-z0-9-]+)? </code></pre> <p>When inserted into your regex, it'd look like this:</p> <pre><code>/^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*$/i </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload