字符串中括号上的Javascript匹配

字符串中括号上的Javascript匹配,javascript,jquery,Javascript,Jquery,如何对包含括号的字符串进行.match匹配? String1.match(“我如何匹配这个(匹配我)”) 没有一个答案能满足我的需求。我可能只是做错了。我试图让我的问题变得基本,我认为这样做会让我的问题变得错误。这是我试图纠正的一个说法: $('[id$=txtEntry3]').focus(function () { if (!DropdownList.toString().match($('[id$=txtEntry2]').val()) || $('[id$=txtEntry2

如何对包含括号的字符串进行.match匹配? String1.match(“我如何匹配这个(匹配我)”)


没有一个答案能满足我的需求。我可能只是做错了。我试图让我的问题变得基本,我认为这样做会让我的问题变得错误。这是我试图纠正的一个说法:

$('[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();
});
$('[id$=txtEntry3]')。焦点(函数(){
如果(!DropdownList.toString().match($('[id$=txtEntry2]')).val())|$('[id$=txtEntry2]')).val().length<5){
$('[id$=txtEntry2]')。选择();
ErrorMessageIn(“您输入的配料编号无效。”);
返回;
}
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;
}
$('[id$=txtEntry3]')。焦点(函数(){
如果(!strip括号(DropdownList).match(strip括号($('[id$=txtEntry2]')).val())|$('[id$=txtEntry2]')).val().length<5){
$('[id$=txtEntry2]')。选择();
如果(!$('[id$=txtEntry2]')。val()=“”){
ErrorMessageIn(“您输入的配料编号无效。”);
}
返回;
}
ErrorMessageOut();
});
函数带括号(字符串){
x=String.toString().replace(/\(/g');
x=x.toString()。替换(/\)/g',);
返回x;
}

使用转义字符-请参见此处:

以获取所有出现的字符,例如“。(匹配我)…(也匹配我)…”添加
g
regexp标志

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

这也是一个优点,即您只能获得所有事件的列表。如果没有此标志,结果将包括整个regexp模式匹配(作为结果数组的第一项)

如果您对括号之间的字符串部分感兴趣,则可以使用
/\([^\)]*)\/
;如果您只需要获取完整字符串,那么您可以使用
/\([^\)]*\)/

使用\来转义(and),就像这样\(and\)当括号包含换行符时,您的解决方案也会匹配。(这是相对的,不管它是不是优势)
string.match(/\((.*?)\)/g)