5 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個贊
您可以測試字符串末尾是否包含/?s=:
var url1 = 'https://example.com/?s=';
var url2 = 'https://example.com/?s=withsearchterms';
var regex = /\/\?s=$/;
console.log(url1 + ' ==> ' + regex.test(url1));
console.log(url2 + ' ==> ' + regex.test(url2));
輸出:
https://example.com/?s= ==> true
https://example.com/?s=withsearchterms ==> false
解釋:
\/\?s=
- 預(yù)計/?s=
$
- 尾隨$
錨定正則表達(dá)式在末尾,例如前面的文本必須出現(xiàn)在末尾因此,如果 url 沒有搜索詞,則測試返回 true(您可以反轉(zhuǎn)
if
測試)

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個贊
這里不需要正則表達(dá)式,這樣的東西在現(xiàn)代瀏覽器中應(yīng)該可以正常工作:
const queries = new URLSearchParams(window.location.search)
if (queries.has("s") && queries.get("s").length == 0){
// do stuff
}

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個贊
如果你想使用 REGEX,你可以使用exec()而不是test()因?yàn)闇y試函數(shù)不適合這種情況。嘗試這個:
//URL-input
var href1 = 'http://example.com/?s='
var href2 = 'http://example.com/?s=xx'
var href3 = 'http://example.com/'
function alertsSearchString( href ){
var regex = /(?<=\/\?s=).*$/
var Container= regex.exec( href )
if ( Container!=null && Container[0]=='' )
alert( 'The search string is an empty string!' )
else if (Container!=null)
alert( 'The search string: ' + Container[0] )
else
alert( "The Container is "
+ Container
+", because input URL isn't matched the \nREGEX : "
+ regex.toString() )
}
//alerts-output
alertsSearchString( href1 )
alertsSearchString( href2 )
alertsSearchString( href3 )
輸出:
First Alert : The search string is an empty string! SecondAlert : The search string: xx Third Alert : The Container is null because input URL isn't matched the REGEX : /(?<=\/\?s=).*$/
細(xì)節(jié):
正則表達(dá)式:(?<=\/\?s=).*$
(?<=\/\?s=)
使用lookbehind來檢查并跳過/?s=
。.*
匹配 后的零到多個字符/?s=
。$
前面的文本必須出現(xiàn)在末尾。
請參閱正則表達(dá)式演示
下面的來源是從您的示例編輯 11/22/2020使用exec()
var search = 'http://example.com/search/?s='
var regex = /(?<=\/\?s=).*$/
var result=regex.exec( search )
if (result && result[0]=='') {
alert("The search terms are empty.");
} else {
alert("The search terms are not empty or no matched.");
}

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個贊
另一種(大部分)避免正則表達(dá)式的替代方案:
function isEmptySearch(urlString) {
const url = new URL(urlString);
const urlParams = url.search.replace(/^\?/, '').split('&').reduce( (acc, cur) => {
const param = cur.split('=');
acc[param[0]] = param[1];
return acc;
}, {});
return !urlParams.s;
}
const testUrls = [
"http://example.com/search/",
"http://example.com/search/?s=",
"http://example.com/search/?s=&foo=bar&baz",
"http://example.com/search/?s=hello&foo=bar&baz"
];
testUrls.forEach( url => console.log(`${url}: empty search = ${isEmptySearch(url)}`) );
我想我更喜歡 Peter Thoeny 之前提出的正則表達(dá)式選項,因?yàn)樗惶唛L,但這個版本可能會引起興趣。

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個贊
忘記正則表達(dá)式,nodejsURL
是你的朋友。https://nodejs.org/dist/latest-v14.x/docs/api/url.html#url_new_url_input_base
對于舊版 Nodejs 版本,您可以使用url.parse
和querystring.parse
const { URL } = require('url');
const url1 = new URL('https://example.com/?s=');
const url2 = new URL('https://example.com/?s=withsearchterms');
function hasEmptyQuery(u) {
return [...u.searchParams]
.some(([key, value]) => value.length === 0);
}
console.log(hasEmptyQuery(url1));
// true
console.log(hasEmptyQuery(url2));
// false
添加回答
舉報