第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Javascript、.test 和 RegEx 評估 URL 中的 /?s=

使用 Javascript、.test 和 RegEx 評估 URL 中的 /?s=

慕碼人2483693 2023-07-20 16:40:43
http://example.com我想在瀏覽器窗口中測試 URL中的空搜索字符串,即 ie http://example.com/search/?s=,但不匹配/search/?s=withsearchterms在 后有任何搜索詞的任何內(nèi)容/search/?s=,然后使用if語句并.addClass顯示一個 div 來警告沒有輸入搜索詞。我正在嘗試使用 Javascript,g.test如下所示;RegEx根據(jù)幾位 RegEx 測試人員的說法,該模式是有效的。但沒有運(yùn)氣:var href = window.location.href;var contains = /[\/?s=]+/g.test(href);if (contains) {$("#no-search-terms").addClass("display-block");}我的正則表達(dá)式錯誤嗎?難道是我的使用方式test不對?編輯 11/29/2020這項工作,感謝Heo:var search = window.location.href;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.");}但miknik 的答案要簡單得多,不需要正則表達(dá)式。適用于 Chrome 87、Firefox 83 和 Safari 14:const queries = new URLSearchParams(window.location.search)if (queries.has("s") && queries.get("s").length == 0){  alert("The search terms are empty.");      }
查看完整描述

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測試)


查看完整回答
反對 回復(fù) 2023-07-20
?
米脂

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

}


查看完整回答
反對 回復(fù) 2023-07-20
?
嗶嗶one

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.");


}


查看完整回答
反對 回復(fù) 2023-07-20
?
侃侃無極

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,但這個版本可能會引起興趣。


查看完整回答
反對 回復(fù) 2023-07-20
?
BIG陽

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.parsequerystring.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


查看完整回答
反對 回復(fù) 2023-07-20
  • 5 回答
  • 0 關(guān)注
  • 219 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號