Regular expression with alphanumeric symbols (case insensitive) and 8 or more characters, but for each language I will also summarize the optimized ones. However, there are some languages that I don't know much about, so please point out any mistakes.
--In JavaScript, ^
$
represents the beginning and end, respectively, unless you specify multiline mode. \ A`` \ z
does not exist in the first place.
--In Ruby, ** multi-line mode ** by default, so ^
$
will match the beginning and end of each line, respectively. This makes no sense for regular expressions, so use \ A`` \ z
to match the beginning and end.
--In PHP, it's not as serious as Ruby, but since $
matches the end or the end ** from a newline **, use \ z
without a newline.
--The \ z
in Ruby and PHP is equivalent to \ Z
in Python.
--In JavaScript, Ruby, and PHP, you can simply add Case-Insensitive matching by adding ʻiafter the delimiter. --In Python, Case-Insensitive is declared by including
(? I)` in the pattern. There is a way to specify it with another argument, but this is simpler.
--Pattern modifiers cannot be used in HTML attributes.
--When using the longest match, limit the character type. --In Ruby and PHP, exclusive quantity specifiers are available. There is no loss in using it, so I will use it.
--In Ruby, you need to escape [
in the character class.
** (2015/06/23 am) ** Added the one that prioritizes execution speed. However, I personally think that it is a regular expression that does not need to sacrifice readability extremely, so I commented it out to make it less noticeable. Consider using it when you need to be concerned about execution speed.
** (2015/06/23 afternoon) **
If you're worried that . *?
Matches an infinite length string in theory, you can be a little relieved just by setting it to. {0,99}?
. This change wouldn't make it much less readable.
This is the simplest pattern. I'm a little worried when asked if this is all right in this era ...
JavaScript 
/^[a-z\d]{8,100}$/i
HTML Pattern Attribute
"^[a-zA-Z\d]{8,100}$"
Ruby
/\A[a-z\d]{8,100}+\z/i
PHP
'/\A[a-z\d]{8,100}+\z/i'
Python
'\A[a-z\d]{8,100}\Z(?i)'
By using affirmative look-ahead, it is possible to make a statement that ** contains at least one character. Negative look-ahead like Verify whether half-width alphanumeric characters and half-width symbols are used in passwords in VBScript regular expressions It can be realized by using it, but even when the number of character types increases, affirmative look-ahead can be described more simply.
JavaScript 
/^(?=.*?[a-z])(?=.*?\d)[a-z\d]{8,100}$/i
# /^(?=\d{0,99}[a-z])(?=[a-z]{0,99}\d)[a-z\d]{8,100}$/i
HTML Pattern Attribute
"^(?=.*?[a-zA-Z])(?=.*?\d)[a-zA-Z\d]{8,100}$"
# "^(?=\d{0,99}[a-zA-Z])(?=[a-zA-Z]{0,99}\d)[a-zA-Z\d]{8,100}$"
Ruby
/\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{8,100}+\z/i
# /\A(?=\d{0,99}+[a-z])(?=[a-z]{0,99}+\d)[a-z\d]{8,100}+\z/i
PHP
'/\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{8,100}+\z/i'
# '/\A(?=\d{0,99}+[a-z])(?=[a-z]{0,99}+\d)[a-z\d]{8,100}+\z/i'
Python
'\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{8,100}\Z(?i)'
# '\A(?=\d{0,99}[a-z])(?=[a-z]{0,99}\d)[a-z\d]{8,100}\Z(?i)'
An increasing number of web services are requiring both case. Currently, I think this is the most ideal.
JavaScript 
/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]{8,100}$/
# /^(?=[A-Z\d]{0,99}[a-z])(?=[a-z\d]{0,99}[A-Z])(?=[a-zA-Z]{0,99}\d)[a-zA-Z\d]{8,100}$/
HTML Pattern Attribute
"^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]{8,100}$"
# "^(?=[A-Z\d]{0,99}[a-z])(?=[a-z\d]{0,99}[A-Z])(?=[a-zA-Z]{0,99}\d)[a-zA-Z\d]{8,100}$"
Ruby
/\A(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]{8,100}+\z/
# /\A(?=[A-Z\d]{0,99}+[a-z])(?=[a-z\d]{0,99}+[A-Z])(?=[a-zA-Z]{0,99}+\d)[a-zA-Z\d]{8,100}+\z/
PHP
'/\A(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]{8,100}+\z/'
# '/\A(?=[A-Z\d]{0,99}+[a-z])(?=[a-z\d]{0,99}+[A-Z])(?=[a-zA-Z]{0,99}+\d)[a-zA-Z\d]{8,100}+\z/'
Python
'\A(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]{8,100}\Z'
# '\A(?=[A-Z\d]{0,99}[a-z])(?=[a-z\d]{0,99}[A-Z])(?=[a-zA-Z]{0,99}\d)[a-zA-Z\d]{8,100}\Z'
I wonder if this is still so popular ...
JavaScript 
/^(?=.*?[a-z])(?=.*?\d)(?=.*?[!-\/:-@[-`{-~])[!-~]{8,100}$/i
# /^(?=[!-@[-`{-~]{0,99}[a-z])(?=[!-\/:-~]{0,99}\d)(?=[a-z\d]{0,99}[!-\/:-@[-`{-~])[!-~]{8,100}$/i
HTML Pattern Attribute
"^(?=.*?[a-zA-Z])(?=.*?\d)(?=.*?[!-/:-@[-`{-~])[!-~]{8,100}$"
# "^(?=[!-@[-`{-~]{0,99}[a-zA-Z])(?=[!-/:-~]{0,99}\d)(?=[a-z\d]{0,99}[!-/:-@[-`{-~])[!-~]{8,100}$"
Ruby
/\A(?=.*?[a-z])(?=.*?\d)(?=.*?[!-\/:-@\[-`{-~])[!-~]{8,100}+\z/i
/\A(?=.*?[a-z])(?=.*?\d)(?=.*?[!-~&&[^a-z\d]])[!-~]{8,100}+\z/i
%r#\A(?=.*?[a-z])(?=.*?\d)(?=.*?[!-/:-@\[-`{-~])[!-~]{8,100}+\z#i
# /\A(?=[!-@\[-`{-~]{0,99}+[a-z])(?=[!-\/:-~]{0,99}+\d)(?=[a-z\d]{0,99}+[!-\/:-@\[-`{-~])[!-~]{8,100}+\z/i
# %r#\A(?=[!-@\[-`{-~]{0,99}+[a-z])(?=[!-/:-~]{0,99}+\d)(?=[a-z\d]{0,99}+[!-/:-@\[-`{-~])[!-~]{8,100}+\z#i
PHP
'/\A(?=.*?[a-z])(?=.*?\d)(?=.*?[!-\/:-@[-`{-~])[!-~]{8,100}+\z/i'
'#\A(?=.*?[a-z])(?=.*?\d)(?=.*?[!-/:-@[-`{-~])[!-~]{8,100}+\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