我想解析字符串并获得匹配,如下所示:
var string = `{{type|equal:'user','This is a {{type}}','{{type}} not authorized to perform "{{current_action}}" action'}}`;
所需的输出:
[
{
match: '{{type|equal:'user','This is a {{type}}','{{type}} not authorized to perform "{{current_action}}" action'}}',
key: 'type',
},
{
match: '{{type}}',
key: 'type'
},
{
match: '{{current_action}}',
key: 'current_action'
}
]
这是我尝试过的:
var string = `{{type|equal:'user','This is a {{type}}','{{type}} not authorized to perform "{{current_action}}" action'}}`;
var regex = RegExp('{{(.*?)}}', 'g');
var match;
var matches = [];
while ((match = regex.exec(string)) !== null) {
matches.push({match: match[0], key: match[1]});
}
console.log(matches);