regex - 使用正则表达式模式会产生编译错误“无效的正则表达式”
问题描述
我正在尝试在我的 Angular 应用程序中使用来自https://www.regextester.com/94502的正则表达式 。
所以我复制了正则表达式并将其放在两个 / 字符之间......
websiteRegex = /?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'^(?:http(s)\(\)\*\+,;=.]+$/;
website: ['', [Validators.maxLength(250), Validators.pattern(this.websiteRegex)]]
但这会导致编译错误:
Module parse failed: Invalid regular expression: /?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'^(?:http(s)\(\)\*\+,;=.]+$/: Nothing to repeat
我怎样才能解决这个问题?
解决方案
使用正确的正则表达式时工作正常...
websiteRegex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;
推荐阅读
- typescript - 在 Typescript 中扩展泛型类型
- next.js - NextJS 中的 FOUC 使用 Tailwind Css
- android-tv - BrowseFragment 中的 Badge Drawable 未出现在 Amazon TV 上
- scala - 如何使用 CompareTo 从 scala 中的流中查找最大日期?
- java - 有没有办法扩展 Spring Service 以便在不复制代码的情况下管理继承的实体?
- spring - Spring 安全外部配置
- python - 将极地立体投影中的数据重新映射到经纬度
- sdl-2 - 有什么区别:SDL_FillRect VS SDL_RenderFillRect?
- html - 如何从本地路径上传css文件?
- javascript - 如何以角度实现翻转时钟