1

我有这几行我想匹配,现在我使用多个正则表达式来匹配它们,但是我想知道是否可以在一个正则表达式中匹配两者:

@@Author:logan
//and these, three variations
@@tags:markdown,github,repetitivetag, tagwithsubtags:subtag, another:subtag:subtag2:repeating:this:repeating,repetitivetag,repetitivetag:withsubby,repetitivetag:withsubtag
@@tags:markdown;github;repetitivetag;tagwithsubtags:subtag,another:subtag:subtag2:repeating:this:repeating;repetitivetag;repetitivetag:withsubby;repetitivetag:withsubtag
@@tags:markdown;git-hub;repetitive-tag;tag_with_sub-tags:sub_tag,another:sub_tag:sub-tag2:repeating:this:repeat-_-_-ing;repetitive-tag;repetitive_tag:with_subby;repetitive_tag:with_subtag

我首先要做的是匹配@@NAME:VALUE部分:

  • /^(?:@@)(\w+):(.*.)(?:\n+|$)/gm

假设第一组是NAME,第二组是VALUE

如果NAME是,tags那么我匹配以下正则表达式VALUE

  • /(\w+)((?=,|;)|(:\w[\w|\-]+\w){0,}|)/g

这匹配了几个类似TAG;TAG;TAG ...或我们之前匹配TAG,TAG,TAG ...的组VALUE

然后我将每个TAG与此匹配以获得SUBTAG

  • /(:)(\w[\w|\-]+\w)(?=:|)/g

现在匹配我们上面匹配:SUBTAG:SUBTAG:SUBTAG ...的组TAG

总之

我要匹配

  • (@@)(NAME)(:)(VALUE)
    • (TAG)(;)(TAG)(;)(TAG) ...价值
      • (:)(SUBTAG)(:)(SUBTAG))(;)在标签中

例子

  1. @@Author:logan应该可以得到Name = AuthorValue = logan

  2. 如果值是多个,比如如果它用逗号或分号分隔,那么匹配类似的东西@@tags:tag1;tag2应该能够得到

    Name = Tags, `值 = ['tag1','tag2']

  3. 如果 value 有一个子值,例如

    @@Author:logan:lastname

    或以此为预期目的

    @@Tags:tag1:subtag;tag2:subtag1:subtag2应该能够得到:

    Name = Author,Value = [{logan : [lastname]}]

    Name = Tags,Value = [{tag1 : [subtag]}, {tag2 : [subtag1, subtag2]}]

我如何匹配组内的组,并且只有当它们存在时?

4

1 回答 1

2

这正是您想要的输出:

// Examples:
var a='@@Author:logan';
var b='@@tags:tag1;tag2';
var c='@@Author:logan:lastname';
var d='@@Tags:tag1;tag2:subtag1:subtag2';
var hard1='@@tags:markdown,github,repetitivetag, tagwithsubtags:subtag, another:subtag:subtag2:repeating:this:repeating,repetitivetag,repetitivetag:withsubby,repetitivetag:withsubtag';
var hard2='@@tags:markdown;github;repetitivetag;tagwithsubtags:subtag,another:subtag:subtag2:repeating:this:repeating;repetitivetag;repetitivetag:withsubby;repetitivetag:withsubtag';
var hard3='@@tags:markdown;git-hub;repetitive-tag;tag_with_sub-tags:sub_tag,another:sub_tag:sub-tag2:repeating:this:repeat-_-_-ing;repetitive-tag;repetitive_tag:with_subby;repetitive_tag:with_subtag';

function tags(a){
    // Gets the name:
    var name=a.match(/^@@.*?(:|.$)/);
    if(!name) return;
    var temp=a.indexOf(':')+1;
    name=name[0].substring(2).replace(':','');
    // Returns the name if thats all there is:
    if(!temp) return name;
    a=a.substring(temp);
    // Gets the value:
    var value=a.split(/[,;]/);
    if(value.length==1&&value[0].indexOf(':')==-1)
        value=value[0];
    else for(var i=0;i<value.length;i++) if(value[i].indexOf(':')!=-1) {
        // Gets the subtags if they exist:
        temp={};
        a=value[i].split(':');
        // .shift() will remove/return the first of the array
        temp[a.shift()]=a;
        value[i]=temp;
    }
    return {name:name,value:value};
}
console.log([tags(a),tags(b),tags(c),tags(d),tags(hard1),tags(hard2),tags(hard3)]);

实际上,这真的很酷。我不确定您的问题是否正是您想要的,但如果没有,那么一切都应该很容易修改。希望你喜欢 else-for-if 语句!

于 2013-01-13T10:48:54.677 回答