简体   繁体   中英

Regex to match the data between the double quotes

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

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.

Use

 const string = `1. "Settings Icon": "xxhdjxxx7wn" 2. "Settings Icon": "afewfwl" 3. "Settings Icon": "fdj ssde"` console.log( string.match(/(?<=")[^"]+(?=":)/g) )

DEMO

在此处输入图像描述

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM