2017-04-21 56 views
-3

字符串的属性:我需要一个正则表达式匹配以下字符串

{title id="fsfdsfsdf" link1="http://google.com" link1_title="Some link"} 

什么属性值,我需要:

fsfdsfsdf, http://google.com, Some link 

我需要他们可能是在捕获组()

正则表达式实际上是我的弱点,所以感谢任何人提出了一个方法来做到这一点,或者一些进一步的资源,最终可以帮助我解决这个问题。

+3

请分享你尝试过的代码。 –

+0

...和一些正确的数据。这是一个字符串? –

+0

由于本网站的格式,您可能最好的服务方式是通过在线RegEx教程以及StackOverflow上的[RegEx文档](http://stackoverflow.com/documentation/regex/topics)阅读此处 –

回答

0

你可以做这样的事情:

(\w+)(?==) 

Live Demo

编辑

因为它的属性值,而不是你想要的键。然后,你可以这样做:

="((?:\\"|[^"])*?)" 

Live Demo

var input = "{title id=\"fsfdsfsdf\" link1=\"http://google.com\" link1_title=\"Some link\"}"; 
 

 
const re = /="((?:\\"|[^"])*?)"/g; 
 

 
match = re.exec(input); 
 

 
while (match != null) { 
 
    console.log(match[1]); 
 
    match = re.exec(input); 
 
}

,输出:

fsfdsfsdf 
http://google.com 
Some link 
+0

谢谢非常多的答案。不幸的是,也许我的问题还不够清楚,我需要属性“值”而不是属性名称本身。任何想法? – Zota

+0

@Zota看看我的编辑。 – Vallentin

+0

这真是一个很好的起点,可以构建我自己的100%工作正则表达式: '^ \ {title id =“((?:\\”| [^“])*?)”\ slink1 =“((? :\\“| [^”])*?)“\ slink1_title =”((?:\\“| [^”])*?)“[\ s^\}] $'。我需要匹配整个字符串,然后有attr。值 – Zota