javascript - 正则表达式检查第一个字符为 + 和其他只是数字
问题描述
我的用例是验证字符串仅包含 + 和数字。我尝试了很多正则表达式,但无法解决我的问题。
我使用/^[0-9]+$/
了正则表达式,但在第 2 次和第 3 次(显然是因为它有一个字符)输入失败了。
我的需要是,
if the input is 00000094777216903 - TRUE
if the input is +947777216903 - TRUE (+ is fine if it is in the beginning of the string)
if the input is +947777216903A - FALSE
if the input is 0000777216903A - FALSE
任何人都可以帮助我实现我在 JS 中的需求。
解决方案
试试这个正则表达式 https://regex101.com/r/6wW84L/2
/(\+){0,1}[0-9]+$/
您在下面有一个工作示例 This /(+){0,1}[0-9]+$/ not //(+){0,1}[0-9]+$//
推荐阅读
- c# - Newtonsoft.Json.JsonSerializationException “无法反序列化当前 JSON 对象”
- angular - 让 npm 缓存适用于 azure devops 中的 .net core angular 项目
- sed - 如何防止在 sed 中被后续的 s///(替换的空模式)替换?
- python - Webscraping循环Python和Beautiful Soup中的错误
- node.js - Nest.js 中的嵌套拦截器
- c# - 在 C# 中为 Azure 应用服务将 RTF 文档转换为 PDF 文档
- swift - 使用绑定从常规类函数修改 SwiftUI @State
- node.js - Model.create 不创建文档,但被解析为一个承诺
- c# - 为什么这些方法会无限递归?
- sql - 是否可以将数据库(MS SQL Server、Postgres 等)中的数据直接提取到 LaTeX 中?