正则表达式检查字符串中是否存在 http 或 https
Regex to check if http or https exists in the string
所以我有这个代码:
function validateText(str)
{
var tarea = str;
var tarea_regex = /^(http|https)/;
if(tarea_regex.test(String(tarea).toLowerCase()) == true)
{
$('#textVal').val('');
}
}
这非常适合此:
https://hello.com
http://hello.com
但不适用于:
这是一个 http://hello.com Asdasd Asdasdas 的网站
尝试做一些阅读,但我不知道在哪里放置 * ? 因为他们会根据此处检查字符串上任何位置的表达式 -> http://www.regular-expressions.info/reference.html
谢谢
试试这个:
function validateText(string) {
if(/(http(s?)):'/'//i.test(string)) {
// do something here
}
}
从外观上看,您只是在检查字符串中是否存在http或https。正则表达式对于这个目的来说有点矫枉过正。使用以下indexOf
尝试此简单代码:
function validateText(str)
{
var tarea = str;
if (tarea.indexOf("http://") == 0 || tarea.indexOf("https://") == 0) {
// do something here
}
}
开头
的^
与字符串的开头匹配。只需将其删除即可。
var tarea_regex = /^(http|https)/;
应该是
var tarea_regex = /(http|https)/;
((http(s?))'://))
这里有很多想法:http://regexlib.com/Search.aspx?k=URL&AspxAutoDetectCookieSupport=1
您是否尝试过使用换字符而不是行首字符?
var tarea_regex = /'b(http|https)/;
它似乎做了我认为你想做的事。看这里: http://jsfiddle.net/BejGd/
相关文章:
- web配置http到https重定向异常
- HTML:从http到https发布表单
- 维护https和http之间的私钥
- 组合Bookmarklets以在HTTP和HTTPS之间创建切换
- 调用 http url 有效,但 Https 不起作用
- 当服务器从 http 更改为 https 时,有哪些可能的方法可以解决问题
- HTTP 或 HTTPS 根据当前连接
- 如何在 http:// 页面中加载登录 https:// 页面
- Firefox扩展不适用于http://https://以外的URL
- HTML/JavaScript - 带有自动 http/https 前缀的远程 img src
- 替换 Base64 - http/https 通信是否 8 位干净
- 想要在我的 GWT 应用程序 (Java 7) 中为每个 http/https 超链接添加锚 html 标记
- Facebook http / Https error
- http/https与网站上的linkedin推荐不匹配
- 验证URL(如果用户使用http:// https:// www)
- Safari和Chrome之间的http / https引用
- 为正则表达式增加http/https检查
- Variable HTTP/HTTPS Express Server
- 在Javascript中使用//www.example.com是否自动选择了http/https协议
- mikeal's请求与Node.js中http/https模块的比较