如何使用正则表达式检查PHP或jQuery中有两个固定大写字母后跟数值的值


How would I use regex to check for a value that has two fixed uppercase letters followed by numeric values in PHP or jQuery?

例如"SU1203"或"UP1234"或任何两个字母后面跟着数值。由于

这可以用非常简单的表达式

解决
^[A-Z]{2}'d+$

在JavaScript中你可以使用test()方法:

if(/^[A-Z]{2}'d+$/.test(str))

在PHP中,preg_match:

if(preg_match('/^[A-Z]{2}'d+$/', $str) === 1)

我建议学习正则表达式

参见:

    JavaScript中的正则表达式PHP中的正则表达式

尝试:

if (preg_match('|^[A-Z]{2}[0-9]+$|', $string_to_test))
{
  // matched
}

javascript示例:

if ( /(^[A-Z]{2}'d+)/.test('SU1203') ) {
   alert( 'matched' )
} else { alert('not matched') }