简体   繁体   中英

Capture quoted string in comma separated string using regular expression

I am trying to match all the string withing double quotes which is separated by comma For eg in the sample string

"COUNT","count(1)","crmuser.accounts"

i want to match

COUNT
count(1)
crmuser.accounts

Regular expression (?<=").*?(?=") is matching the comma seperator as well which is not required. How can i exclude commas in the string.

There are a few different workarounds you could use for this; for example using capture groups as suggested in the comments by The fourth bird. But for me personally, I try to avoid using .* .

For this type of example, I would honestly recommend just using a character set of valid characters you will use and looking for more than one character (2 or more) because you will effectively skip lone separators such as a single , . You can even add a comma to that character set and it will still work.

(?<=")[\w\(\)\.,]{2,}(?=")

Demo

For this format of string you could just use :

var str = '"COUNT","count(1)","crmuser.accounts"' ;
var separated = str.split('","') ;
for(var i in separated){
     i.replace('"','') ;
} ;

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