Javascript 多值匹配

Javascript 多值匹配,javascript,regex,Javascript,Regex,我有一根这样的线: str = 'autocomplete=\\\"off\\\" name=\\\"composer_session_id\\\" value=\\\"1557423901\\\" \\\/>\\u003cinput type=\\\"hidden\\\" autocomplete=\\\"off\\\" name=\\\"is_explicit_place\\\" id=\\\"u436754_5\\\"'; 和一个regexp匹配: str.match(/(comp

我有一根这样的线:

str = 'autocomplete=\\\"off\\\" name=\\\"composer_session_id\\\" value=\\\"1557423901\\\" \\\/>\\u003cinput type=\\\"hidden\\\" autocomplete=\\\"off\\\" name=\\\"is_explicit_place\\\" id=\\\"u436754_5\\\"';
和一个regexp匹配:

str.match(/(composer_session_id|is_explicit_place)\\" (?:value|id)=\\"([_a-zA-Z0-9]+)\\"/g)
它工作正常(有点),但尽管如此,我还是使用捕获组来获取
(composer\u session\u id | is\u explicit\u place)
([\u a-zA-Z0-9]+)
结果数组只包含两个元素(匹配字符串中最大的元素):

我错过了什么

如何使用regexp在一次运行中获取字符串:composer\u session\u id、is\u explicit\u place、1557423901和u436754\u 5


解释为什么只返回两个字符串以及获取所需值的解决方案的额外要点不涉及使用
split()
和/或
replace()

如果regex与g标志一起使用,method string.match只返回匹配的数组,它不包括捕获的组。方法RegExp.exec返回最后匹配的数组和最后匹配中捕获的组,这也不是解决方案。 为了以一种相对简单的方式实现您所需要的功能,我建议研究一下replacer函数:

<script type="text/javascript">
    var result = []

    //match - the match
    //group1, group2 and so on - are parameters that contain captured groups
    //number of parameters "group" should be exactly as the number of captured groups
    //offset - the index of the match
    //target - the target string itself
    function replacer(match, group1, group2, offset, target)
    {
        if (group1 != "")
        {
            //here group1, group2 should contain values of captured groups
            result.push(group1);
            result.push(group2);
        }
        //if we return the match 
        //the target string will not be modified
        return match;
    }

    var str = 'autocomplete=\\\"off\\\" name=\\\"composer_session_id\\\" value=\\\"1557423901\\\" \\\/>\\u003cinput type=\\\"hidden\\\" autocomplete=\\\"off\\\" name=\\\"is_explicit_place\\\" id=\\\"u436754_5\\\"';
    //"g" causes the replacer function 
    //to be called for each symbol of target string
    //that is why if (group1 != "") is needed above
    var regexp = /(composer_session_id|is_explicit_place)\\" (?:value|id)=\\"([_a-zA-Z0-9]+)\\"/g;
    str.replace(regexp, replacer);
    document.write(result);
</script>

var结果=[]
//比赛-比赛
//group1、group2等-是包含捕获的组的参数
//参数“组”的数量应与捕获的组的数量完全相同
//偏移量-匹配的索引
//target-目标字符串本身
函数替换程序(匹配、组1、组2、偏移、目标)
{
如果(组1!=“”)
{
//在这里,group1、group2应该包含捕获的组的值
结果:推挤(第1组);
结果:推挤(第2组);
}
//如果我们还比赛
//不会修改目标字符串
复赛;
}
var str='autocomplete=\\\\\“off\\\”name=\\\\“composer\\ u session\\\\”value=\\\\“1557423901\\\”\\\\\/>\\u003cinput type=\\\\“hidden\\\”autocomplete=\\\\\\\“off\\\\\”name=\\\\\\”是显式的\\\\\\\\\\\\\\\\\\“id=\\”u436754\\\”u5\\”;
//“g”导致replacer函数
//要为目标字符串的每个符号调用
//这就是为什么上面需要使用if(group1!=“”)
var regexp=/(编写器会话id为显式位置)\\”(?:值id)=\\”([\u a-zA-Z0-9]+)\\”/g;
str.replace(regexp,replacer);
记录(结果);

你知道你可以嵌套捕获组吗?是的,我知道-这对我有什么帮助?你能给出未转换的原始字符串和正则表达式吗?反斜杠被转义是有原因的。如果反斜杠不被转义,结果字符串将不包含:
\”
,因为JS会将单个反斜杠视为
的转义“
character。作为旁注-这不是真的,String.match()只返回匹配数组并忽略捕获组。请看下面的示例:。不带“g”标志的regexp包含捕获组(唯一的问题是-这是第一个匹配,因为没有全局标志:)@WTK,是的,你说得对,我在string.match方法上搞错了。@WTK更新:string.match with g确实返回了不带捕获组的匹配数组,这就解释了为什么你得到了你在问题中指定的数组,没有g-你是怎么说的。
<script type="text/javascript">
    var result = []

    //match - the match
    //group1, group2 and so on - are parameters that contain captured groups
    //number of parameters "group" should be exactly as the number of captured groups
    //offset - the index of the match
    //target - the target string itself
    function replacer(match, group1, group2, offset, target)
    {
        if (group1 != "")
        {
            //here group1, group2 should contain values of captured groups
            result.push(group1);
            result.push(group2);
        }
        //if we return the match 
        //the target string will not be modified
        return match;
    }

    var str = 'autocomplete=\\\"off\\\" name=\\\"composer_session_id\\\" value=\\\"1557423901\\\" \\\/>\\u003cinput type=\\\"hidden\\\" autocomplete=\\\"off\\\" name=\\\"is_explicit_place\\\" id=\\\"u436754_5\\\"';
    //"g" causes the replacer function 
    //to be called for each symbol of target string
    //that is why if (group1 != "") is needed above
    var regexp = /(composer_session_id|is_explicit_place)\\" (?:value|id)=\\"([_a-zA-Z0-9]+)\\"/g;
    str.replace(regexp, replacer);
    document.write(result);
</script>