简体   繁体   English

正则表达式提取多个值

[英]Regular Expression Extract Multiple Values

/[.#]\\s*([^{\\s]*)\\s*{/ Will get the text between the # or . /[.#]\\s*([^{\\s]*)\\s*{/将获取#或之间的文本。 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. 注意:我只在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: String.split通常更易于用于提取,甚至清理助手也很有用:

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"]

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

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