简体   繁体   中英

Regular Expression Extract Multiple Values

/[.#]\\s*([^{\\s]*)\\s*{/ Will get the text between the # or . and {. I am trying to extract the text between the # or . and { as well as the text between the { and }. I believe it would be similar to the expression bellow.

var productText = '#id {} .class {}';
var m, r = /([.#]\s*([^{\s]*)\s*{)(([^{]*)}/)/g;

You can use this regex to get the part between # and {}

^\#(\S+\s+)\{\}.*

NOTE: I have tried the regex only in Perl. Also for remaining part of the regex i have simply put .* and not extracting the same.

String.split is often easier to use for extraction, and even then a cleanup helper is useful:

var productText = '#id {123} .class {456}';
var m, r = /(:?[.#])([\w\W]+?)\s*\{([\w\W]+?)\s*\}/g;

productText.split(r).slice(2,-1).filter(/./.test, /\w/)
// output array: ["id", "123", "class", "456"]

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