gpt4 book ai didi

javascript - 如何使用 JS/jQUery 正则表达式从字符串中删除不匹配的部分

转载 作者:行者123 更新时间:2023-11-29 23:46:32 24 4
gpt4 key购买 nike

我有正则表达式 /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a- zA-Z0-9@:%_\+.~#?&\\=]*)/g 用于检查有效的 URL。但它接受:

https://www.google.com
google.com
https://google.com
dev.google.com

这对我来说很好,但我希望当用户输入这些值时,过滤后的值将是 google.com,我必须通过 AJAX,但输入是相同的在 UI 部分(指浏览器)。我知道这是可能的,但我不能。

任何引用都会有帮助
提前致谢

最佳答案

尝试

var regExp = /\w+\.\w+(?=$|\/)/;
var matches1 = regExp.exec("https://www.google.com/images");
var matches2 = regExp.exec("google.com");
var matches3 = regExp.exec("https://google.com");
var matches4 = regExp.exec("dev.google.com");


//matches1[0] contains the value between the parentheses
console.log(matches1[0]);
console.log(matches2[0]);
console.log(matches3[0]);
console.log(matches4[0]);

以上所有内容都会为您提供“google.com”

关于javascript - 如何使用 JS/jQUery 正则表达式从字符串中删除不匹配的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43807280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com