I have a regular expression for a postal address in which I need to add length checks: 1 - part before dog, 2 - part after dog, 3 - total length of the whole field. I tried to use forward looking, but, unfortunately, did not work. One special condition - the regular expression should remain as it is, you just need to add the necessary length checks. The expression looks like this:

^((\"[^\"]+\")|([\w\!\#\$\%\^\&\*\(\)\_\+\}\{\-\|\'\/\?\)\(\u0400-\u04FF\~\=\`]+\.)*([\w\!\#\$\%\^\&\*\(\)\_\+\}\{\-\|\'\/\?\)\(\u0400-\u04FF\~\=\`])+)@(([0-9A-Za-z\u0400-\u04FF][\-\_]{0,1})+(\.))+(([0-9A-Za-z\u0400-\u04FF][\-\_]{0,1})+?)$ 
  • But why? Why check the mailing address? Why check it with an incorrect expression in the end? - vp_arth
  • And what programming language is needed? - Yuri
  • @Yuri, shielded dots? - vp_arth
  • Check email addresses, especially on their own - a bad idea - andreymal
  • one
    even if this expression correctly checked the correctness of the email, I would not leave it as it is. it is impossible to read it because of backslash, which are not needed inside square brackets (except for the case of square brackets and a dash not at the beginning / end) - Mike

1 answer 1

This is done by a regular expression for checking email:

 /^([a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+)@([a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*)$/ 

And searching for the desired values ​​using match :

 function length(val) { var value = val.match(/^([a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+)@([a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*)$/); console.log('До собачки: '+value[1].length+'\nПосле собачки: '+value[2].length+'\nВесь размер: '+value[0].length) } 
 <input type="email" id="email"> <button onclick="length(document.getElementById('email').value)">Проверить</button> 

Here is a variant with your regular expression, but it is not correct:

 function length(val) { var value = val.match(/^((\"[^\"]+\")|([\w\!\#\$\%\^\&\*\(\)\_\+\}\{\-\|\'\/\?\)\(\u0400-\u04FF\~\=\`]+\.)*([\w\!\#\$\%\^\&\*\(\)\_\+\}\{\-\|\'\/\?\)\(\u0400-\u04FF\~\=\`])+)@(([0-9A-Za-z\u0400-\u04FF][\-\_]{0,1})+(\.))+(([0-9A-Za-z\u0400-\u04FF][\-\_]{0,1})+?)$/); var all = value[0].length, part_one = value[1].length, part_two = value[5].length + value[8].length console.log('До собачки: '+part_one+'\nПосле собачки: '+part_two+'\nВесь размер: '+all) } 
 <input type="email" id="email"> <button onclick="length(document.getElementById('email').value)">Проверить</button> 

  • Thanks a lot for your help! - messiah
  • @messiah, if my answer is correct, then mark it - Yuri