Keekz
Keekz

Reputation: 185

Check if a word is enclosed within quotes using javascript

I have a string like get(3,"No MATCH",obj). I want to check if the word MATCH is enclosed within quotes (either single quote or double quote). Here MATCH is enclosed within quotes, although not exactly as "MATCH", it is still a part of the text contained in quotes as "No MATCH".
I want to write a function which takes the word (MATCH) as argument and returns true if it is contained within quotes or false if not.
Following are some other input strings which needs to be checked by this function:
* get(1101,"MATCH",obj) --> return true since MATCH is within quotes
* get(255,'NO MATCH',obj) --> return true since MATCH is a part of text contained within quotes
* get(1111,"" , MATCH) ---> return false since MATCH is not contained within quotes

Upvotes: 0

Views: 1152

Answers (3)

customcommander
customcommander

Reputation: 18901

If you're trying to perform syntactic analysis you should consider using esprima

This is the AST returned by

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"
}

And here's for

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"
}

You can search a 'Literal' type which value includes '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>

Upvotes: 0

grodzi
grodzi

Reputation: 5703

I guess this is for refactoring, so this should do

  • no mismatching quotes (maybe except ill stuff like 'MAT\'CH' or 'MAT' + 'CH'
  • single or double quotes

Left handled cases can be handled by hand

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

Upvotes: 1

mplungjan
mplungjan

Reputation: 177701

Have a go with this

https://regex101.com/r/Bu4LUO/2

/['"]([ \w]+)?MATCH([ \w]+)?["']/gm

Upvotes: 1

Related Questions