我想解析字符串并得到这样的匹配:
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);