Home > OS >  Regex to match the data between the double quotes
Regex to match the data between the double quotes

Time:10-01

I have below strings

1. "Settings Icon": "xxhdjxxx7wn"
2. "Settings Icon": "afewfwl"
3. "Settings Icon": "fdj ssde"

I want the regex to match all these strings by ignoring the content in right side of colon

I have (['"])(?:(?!\1|\\).|\\.)*\1 which matches with content inside double quote. But couldn't solve above case.. Please help

CodePudding user response:

If I understand you're question correctly, you only want the string within the quotes, from the left side of the colon. Is that correct? If so, this might work for you:

(['"])([^\1]*?)\1:.*$

For this expression, capture group 2 will give you what you want.

CodePudding user response:

Use

const string = `1. "Settings Icon": "xxhdjxxx7wn"
2. "Settings Icon": "afewfwl"
3. "Settings Icon": "fdj ssde"`

console.log( string.match(/(?<=")[^"] (?=":)/g) )

DEMO

enter image description here

EXPLANATION

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  [^"]                     any character except: '"' (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    ":                       '":'
--------------------------------------------------------------------------------
  )                        end of look-ahead

See regex proof.

  • Related