javascript - 使用正则表达式对 URL 进行 Trix 验证

标签 javascript regex string trix

我正在尝试更改验证模式以接受这些开始

https://
http://
https://www.
http://www. 
www.

这是我现在拥有的:

inputElement.pattern = '^(https:?//|http:?//|http:?//www|https:?//www|www).+$';

它接受错误的情况,例如:

wwwsa.se
http://ww.

使用 https://github.com/basecamp/trix/issues/624 达到这一点

最佳答案

你可以试试这个

^(?:(?:(?:https?:\/\/)?w{3})(?:[.].*|)|(?:https?:?\/\/(?!w{1,2}\.).*))$

enter image description here

Regex Demo

const regex = /^(?:(?:(?:https?:\/\/)?w{3})(?:[.].*|)|(?:https?:?\/\/(?!w{1,2}\.).*))$/i;
const arr = ['https://','http://','https://www.','http://www.','www.','wwwsa.se','http://ww.','http://www.','ww.','www.','www//','www.g', 'http://wix']

arr.forEach(str => {
  console.log(str, '\t\t' ,regex.test(str))
})

关于javascript - 使用正则表达式对 URL 进行 Trix 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58114545/

相关文章:

javascript - JavaScript 中正则表达式的递归匹配

java - 当没有调用 'matching' 方法时,Matcher 抛出 IllegalStateException 的理由

C# 从字符串末尾删除子字符串

sql - 如何连接多行?

javascript - 无法使用 res.send() 使用 express with node 发送数字

php - 更改/修改 Facebook 'Like it' 按钮的样式

javascript - 左侧菜单关闭时如何刷新 Canvas 图表?

javascript - 仅在具有指定类别的图像上显示 Pinterest 悬停按钮

javascript - Javascript 中用于分隔单词的正则表达式

string - Swift 中重载赋值运算符的替代方法