簡體   English   中英

Javascript匹配字符串中的括號

[英]Javascript Match on parentheses inside the string

如何對字符串中帶圓括號的字符串執行.match操作? String1.match(“我如何匹配(MATCH ME)”);


沒有一個答案能讓我得到我想要的東西。 我可能只是做錯了。 我試圖讓我的問題變得基本,我認為這樣做會讓我的問題出錯。 這是我要修復的聲明:

$('[id$=txtEntry3]').focus(function () {

    if (!DropdownList.toString().match($('[id$=txtEntry2]').val()) || $('[id$=txtEntry2]').val().length < 5) {
        $('[id$=txtEntry2]').select();

        ErrorMessageIn("The Ingredient number you entered is not valid.");
        return;
    }
    ErrorMessageOut();
});

這正確地解決了我遇到的問題,當它試圖匹配“txtEntry2”中包含“()”的條目時。



嗯,它有點破碎,但它適用於我需要它做的事情。 這就是我為解決問題所做的工作:

$('[id$=txtEntry3]').focus(function () {

        if (!StripParentheses(DropdownList).match(StripParentheses($('[id$=txtEntry2]').val())) || $('[id$=txtEntry2]').val().length < 5) {
            $('[id$=txtEntry2]').select();
            if (!$('[id$=txtEntry2]').val() == "") {
                ErrorMessageIn("The Ingredient number you entered is not valid.");
            }
            return;
        }
        ErrorMessageOut();
    });

function StripParentheses(String){
    x = String.toString().replace(/\(/g, '');
    x = x.toString().replace(/\)/g, '');
    return x;
}
var str = "How do I match this (MATCH ME)";
str.match(/\((.*?)\)/);

得到所有出現在例如“..(匹配我)..(也匹配我)..”添加g regexp標志

string.match(/\((.*?)\)/g)

這也是一個優點,你只得到所有出現的列表。 沒有這個標志,結果將包括一個完整的正則表達式模式匹配(作為結果數組的第一項)

如果你對括號內的字符串部分感興趣,那么你可以使用/\\(([^\\)]*)\\)/ ; 如果你只需要獲取完整的字符串,那么你可以使用/\\([^\\)]*\\)/

使用轉義字符 - 請參閱此處: http//www.javascriptkit.com/jsref/regexp.shtml

暫無
暫無

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

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