[英]How do I use RegExp to search value of var for specific word
这是我的代码,无法正常工作。 我要完成的工作是,如果用户未键入https:// www 。 然后自动为他们添加。 如果用户确实添加https:// www 。 然后不要为他们添加。
Java脚本
var button = document.getElementById('button');
var search = document.getElementById("search");
button.addEventListener("click", function () {
if (search.value !== "https://www.") {
window.open("https://www." + search.value);
} else if (search.value == "https://www.") {
window.open(search.value);
}
})
的HTML
<input id="search" type="text" placeholder="URL"
autocomplete="https://www.">
<button id="button">Search</button>
您可以使用indexOf()
函数来完成此操作。
var searchValue = 'example.com';
if (searchValue.indexOf('https://www') > -1) {
window.open("https://www." + searchValue);
} else {
window.open(searchValue);
}
var button = document.getElementById('button'); var search = document.getElementById("search"); var protocol = /^(http(s)?(:\\/\\/))?(www\\.)?/gi; button.addEventListener("click", function() { if (!search.value.match(protocol)[0]) { window.open("https://www." + search.value); } else if (search.value.match(protocol)[0]) { window.open(search.value); } });
<input id="search" type="text" placeholder="URL" autocomplete="https://www."> <button id="button">Search</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.