使用 javascript 检查单词是否包含在引号内
Check if a word is enclosed within quotes using javascript
我有一个像 get(3,"No MATCH",obj)
这样的字符串。我想检查单词 MATCH 是否包含在引号中(单引号或双引号)。这里的 MATCH 是用引号括起来的,虽然不完全是 "MATCH",但它仍然是引号中包含的文本的一部分,如 "No MATCH"。
我想编写一个函数,它将单词 (MATCH) 作为参数,return如果它包含在引号中则为真,否则为假。
以下是一些其他需要检查的输入字符串
功能:
* get(1101,"MATCH",obj)
--> return 为真,因为 MATCH 在引号内
* get(255,'NO MATCH',obj)
--> return 正确,因为 MATCH 是引号中包含的文本的一部分
* get(1111,"" , MATCH)
---> return false 因为 MATCH 不包含在引号中
试一试
https://regex101.com/r/Bu4LUO/2
/['"]([ \w]+)?MATCH([ \w]+)?["']/gm
我想这是为了重构,所以应该这样做
- 没有不匹配的引号(可能除了诸如 'MAT\'CH' 或 'MAT' + 'CH'
- 单引号或双引号
遗留案件可以人工处理
const inString = s => line => {
const has = [...line.matchAll(/'[^']*'/g)].find(x => x[0].includes(s))
return has || [...line.matchAll(/"[^"]*"/g)].find(x => x[0].includes(s))
}
const matcher = inString('MATCH')
console.log(matcher('get(1101,"MATCH",obj)'))
console.log(matcher("get(255,'YES MATCH',obj)"))
console.log(matcher('get(1111,"" , NO MATCH)')) // not in quotes
console.log(matcher('get(1111,"" , "NO MATCH\')')) // no mismatching quotes
console.log(matcher("get('a',NO MATCH , 'b')")) // wrapped by matching quotes does not match
如果您尝试执行句法分析,您应该考虑使用 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>
我有一个像 get(3,"No MATCH",obj)
这样的字符串。我想检查单词 MATCH 是否包含在引号中(单引号或双引号)。这里的 MATCH 是用引号括起来的,虽然不完全是 "MATCH",但它仍然是引号中包含的文本的一部分,如 "No MATCH"。
我想编写一个函数,它将单词 (MATCH) 作为参数,return如果它包含在引号中则为真,否则为假。
以下是一些其他需要检查的输入字符串
功能:
* get(1101,"MATCH",obj)
--> return 为真,因为 MATCH 在引号内
* get(255,'NO MATCH',obj)
--> return 正确,因为 MATCH 是引号中包含的文本的一部分
* get(1111,"" , MATCH)
---> return false 因为 MATCH 不包含在引号中
试一试
https://regex101.com/r/Bu4LUO/2
/['"]([ \w]+)?MATCH([ \w]+)?["']/gm
我想这是为了重构,所以应该这样做
- 没有不匹配的引号(可能除了诸如 'MAT\'CH' 或 'MAT' + 'CH'
- 单引号或双引号
遗留案件可以人工处理
const inString = s => line => {
const has = [...line.matchAll(/'[^']*'/g)].find(x => x[0].includes(s))
return has || [...line.matchAll(/"[^"]*"/g)].find(x => x[0].includes(s))
}
const matcher = inString('MATCH')
console.log(matcher('get(1101,"MATCH",obj)'))
console.log(matcher("get(255,'YES MATCH',obj)"))
console.log(matcher('get(1111,"" , NO MATCH)')) // not in quotes
console.log(matcher('get(1111,"" , "NO MATCH\')')) // no mismatching quotes
console.log(matcher("get('a',NO MATCH , 'b')")) // wrapped by matching quotes does not match
如果您尝试执行句法分析,您应该考虑使用 esprima
这是
返回的 ASTesprima.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>