簡體   English   中英

正則表達式值不匹配jQuery

[英]regex value is not matching jquery

我正在嘗試將點擊的鏈接路徑與其他鏈接進行匹配。 它根本不起作用。 這是代碼。 我需要鏈接的href以正則表達式開頭。

 $('#taxonomylist ul li a').click(function() {
        $href = $(this).attr("href");
        $regex = new RegExp("^"+$href);
        $('#taxonomylist ul li').each(function() {
        $href_sub = $("a", this).attr("href");
        if ($href_sub.match($regex))
        {
        $(this).css("display", "block");
        }

       });
       return false;
        });

這是我在螢火蟲中遇到的問題:

$href = "/?q=category/activity/test"  
$href_sub = "/?q=category/activity/test/lamp"  
$regex = /^\/?q=category\/activity\/test/  

一切似乎都可以找到,但是不能按預期工作。 如果我通過正則表達式刪除匹配項,則一切正常(當然無需過濾)。

編輯:

現在它可以部分工作,僅將css屬性分配給所有鏈接,而不僅是那些與值匹配的屬性。 有人看到這個問題嗎?

$('#taxonomylist ul li a').click(function() {
$href = $(this).attr("href");
$regex = new RegExp("^"+$href);
$('#taxonomylist ul li').each(function() {
$href_sub = $("a", this).attr("href");
if ('$href_sub:contains($href)')
{
$(this).css("display", "block");
}

});
return false;
});

因為您要問正則表達式是否您的字符串匹配 意思是,您的正則表達式至少應如下所示(根據您的示例):

$regex = /^\/?q=category\/activity\/test\/\w+/

您可能要改用:contains()選擇器。

檢查這個

使用indexOf(//value to search)進行匹配

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM