Если вы пытаетесь выполнить анализ синтаксиса c, вам следует подумать об использовании esprima
Это AST, возвращаемый
esprima.parse(`get(1111,"" , MATCH)`);
{
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "get"
},
"arguments": [
{
"type": "Literal",
"value": 1111,
"raw": "1111"
},
{
"type": "Literal",
"value": "",
"raw": "\"\""
},
{
"type": "Identifier",
"name": "MATCH"
}
]
}
}
],
"sourceType": "script"
}
И вот для
esprima.parse(`get(255,'NO MATCH',obj)`)
{
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "get"
},
"arguments": [
{
"type": "Literal",
"value": 255,
"raw": "255"
},
{
"type": "Literal",
"value": "NO MATCH",
"raw": "'NO MATCH'"
},
{
"type": "Identifier",
"name": "obj"
}
]
}
}
],
"sourceType": "script"
}
Вы можете выполнить поиск по типу 'Literal', значение которого включает 'MATCH'.
const matcher = code => {
const ast = esprima.parse(code);
const args = ast.body[0].expression.arguments;
return args.some(({value, type}) =>
type === 'Literal'
&& typeof value === 'string'
&& value.includes('MATCH'));
};
console.log(matcher(`get(1101,"MATCH",obj)`));
console.log(matcher(`get(255,'NO MATCH',obj)`));
console.log(matcher(`get(1111,"" , MATCH)`));
<script src="https://unpkg.com/esprima@~4.0/dist/esprima.js"></script>