Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
'p' 只能存储“$”、逗号、点或数字。
如果它包含任何其他字符,我如何显示警报?
if (p.match(/[^$,.\d]/)) alert('error!');
现场演示
你可以使用这个 优秀的正则表达式备忘单。
考虑:
if (/[^$,\.\d]/.test(p)) { // value has characters other than $ , . 0-9. };
正则表达式测试方法返回一个布尔值,而match返回一个数组,因此在以类似方式使用时依赖于类型转换。