简体   繁体   中英

Javascript Regex - Match multi-line strings (SCSS Map)

I am currently working on a Node script which will search through a SCSS (Sass) file using regular expressions. The aim is to build an array/arrays of all variables defined and their values. These can then be compared against values documented in the project's documentation to check everything is in sync.

I have used the following regex to match single line variables:

string.match(/\\$[az-]+:(.*)(?=;)/)

and

string.match(/[$a-zA-Z-]+(?=:)/g)[0] to extract each variable's name. However SCSS Maps and SCSS Variables with rather wordy values may spread onto multiple lines.

How can I search for a string which starts with a $ , ends with a ; and may be spread over multiple lines? Ideally I'd like to use multiple capture groups to return a single array with single line variables in one group and SCSS maps in another. This isn't essential though, two regexes would be fine.

If you can use last Node.js versions with ES2018 support, you can try /s flag .

However, for such purposes, an appropriate parser would be better.

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