简体   繁体   English

使用正则表达式查找对象的值

[英]Find the values of an Object using Regex

I am trying to find the values of a JavaScript object using Regex.我正在尝试使用正则表达式查找 JavaScript 对象的值。

Sample object: classes: {wrapper: 'sm-mb--1-half md-mb--2', item: 'sm-mb--quarter sm-mt--quarter'}示例对象: classes: {wrapper: 'sm-mb--1-half md-mb--2', item: 'sm-mb--quarter sm-mt--quarter'}

Expected output: sm-mb--1-half md-mb--2 sm-mb--quarter sm-mt--quarter预期输出: sm-mb--1-half md-mb--2 sm-mb--quarter sm-mt--quarter

What I have so far /\\{([A-Za-z]*:\\s'.*',?\\s?)*\\}/gm , but for some reason it selects everything..到目前为止我所拥有的/\\{([A-Za-z]*:\\s'.*',?\\s?)*\\}/gm ,但由于某种原因它选择了一切..

Sandbox: https://regex101.com/r/hQfHKN/1沙盒: https : //regex101.com/r/hQfHKN/1

Using lookahead and Lookbehind you can achieve what you want.使用lookaheadLookbehind ,你可以达到你想要的。

 let str = `classes: {wrapper: 'sm-mb--1-half md-mb--2', item: 'sm-mb--quarter sm-mt--quarter'}`; const regex = /(?<={.*:).*?(?=,|})/g; console.log(str.match(regex))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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