product_id的值可以是字母和数字的某种组合,例如: GB47NTQQ。
我想检查一下,除了第3个和第4个字符之外,其他字符是否都相同。
类似于:
if product_id = GBxxNTQQ //where x could be any number or letter.
//do things
else
//do other things我如何使用JavaScript来实现这一点?
发布于 2012-01-19 12:18:31
使用正则表达式和string.match()。句点是单个通配符。
string.match(/GB..NTQQ/);发布于 2012-01-19 12:19:08
使用regular expression匹配:
if ('GB47NTQQ'.match(/^GB..NTQQ$/)) {
// yes, matches
}发布于 2012-01-19 12:54:40
到目前为止,答案都建议使用match,但test可能更合适,因为它返回true或false,而match返回null或匹配数组,因此需要在条件中对结果进行(隐式)类型转换。
if (/GB..NTQQ/.test(product_id)) {
...
}https://stackoverflow.com/questions/8920948
复制相似问题