regex - 包含子字符串且没有空格的正则表达式模式
问题描述
我想在 Angular 中验证输入表单,字符串必须包含子字符串:
facebook.com
或者
fb.me
和
no whitespaces
例如:
1) randomString -> Fail
2) www.facebook.com -> Ok
3) www.fb.me -> Ok
4) www.facebook.com/pippo pallino -> Fail (there is a withespace after the word "pippo")
对于前 3 个,我有一些工作模式:
pattern = '^.*(?:facebook\\.com|fb\\.me).*$';
但这并不能验证第四个。
解决方案
您可以使用
pattern = '^\\S*(?:facebook\\.com|fb\\.me)\\S*$';
或者,使用正则表达式文字符号:
pattern = /^\S*(?:facebook\.com|fb\.me)\S*$/;
在这里,.*
替换为\S*
匹配 0 个或更多非空白字符。
在线查看正则表达式演示。
推荐阅读
- ios - 后台任务iOS不触发本地通知
- java - 比较列表的所有成员
- vue.js - vuepress vue提示和文件夹结构
- javascript - 动态地将类添加到字符串中的单词Angular
- python - 如何创建一个独立的非模态对话框
- c# - cplex bool 决策变量返回双精度值
- opencv - (共享) Yolov5 模型中的多任务学习
- c# - 选择项目后如何使下拉按钮的弹出窗口自动关闭?
- windows - 在 Windows Master 和远程 Windows 机器之间的 jenkins 中尝试主从连接
- python - 直接将文件上传到 SFTP 而不将其存储到本地系统中