简体   繁体   English

javascript正则表达式匹配模式(有冲突)

[英]javascript regex matching a pattern (with conflict)

Suppose the following string: 假设以下字符串:

^[message] [site](http://example.com)

Now, I need a regex that extracts "site" and " http://example.com ". 现在,我需要一个提取“ site”和“ http://example.com ”的正则表达式。 I've come up with the following regex: 我想出了以下正则表达式:

/\\[(.*?)\\]\\((.*?)\\)/gm

But this does not exclude [message] from the regex result, returning message] [site instead of site . 但这并不排除正则表达式结果中的[message] ,而是返回message] [site而不是site

I've tried several other possible regex expressions but just can make it to the right one. 我尝试了其他几种可能的正则表达式,但可以使其正确。

Any ideas? 有任何想法吗?

如果该空间始终存在:

/\s\[(.*?)\]\((.*?)\)/gm

When trying to match on the contents within the brackets or parentheses, try and find any character that isn't the end character ( [^\\]] and [^)] ), also *? 当尝试匹配方括号或括号内的内容时,请尝试查找不是结尾字符的任何字符( [^\\]][^)] ),也可以*? could be replaced with + : 可以用+代替:

/\[([^\]]*?)\]\(([^)]*?)\)/gm

Example: Regex101 示例: Regex101

 let str = '^[message] [site](http://example.com)' let re = /\\[([^\\]]*?)\\]\\(([^)]*?)\\)/gm let matches = str.match(re) console.log( RegExp.$1 ) console.log( RegExp.$2 ) 

Based on your string, and using javascript, \\^\\[.*?\\]\\s\\[(.*)\\]\\((.*?)\\)/gm should suffice so long as you always extract group1, group2 , but ignore group0 ... try the below code; 根据您的字符串并使用javascript,只要您始终提取group1,group2\\^\\[.*?\\]\\s\\[(.*)\\]\\((.*?)\\)/gm就足够了,但忽略group0 ...尝试以下代码;

// set up a test string with multiple matches
var text = "daska ^[message] [site](site.com) fjhdfgk fafug ^[message2] [site2](site2.com) fgsadkfgaskf akfkakf";

// collect all matching groups globally
// (this creates an array of full matches which we can water down next)
var results = text.match( /\^\[.*?\]\s\[(.*?)\]\((.*?)\)/gm );
// output: ["^[message] [site](site.com)", "^[message2] [site2](site2.com)"]

// use the full matches to extract the groups we want,
// and then fill an array of matches
results = results.map( function( result ) {
    var match = result.match( /\^\[.*?\]\s\[(.*?)\]\((.*?)\)/ );
    return [ match[1], match[2] ];
});
// output: [["site", "site.com"], ["site2", ["site2.com"]]

You should be able to use the output array to do whatever you require :) 您应该可以使用输出数组执行所需的任何操作:)

\\[(.*?)\\]\\((.*?)\\) Will match message] [site in the first capturing group because it will try to match at least as possible until it can match an opening parenthesis. \\[(.*?)\\]\\((.*?)\\)将匹配message] [site第一个捕获组中的message] [site ,因为它将尝试至少匹配,直到可以匹配开头的括号。

Instead you could use 2 capturing groups first matching ^[message] and after that match start the capturing groups. 相反,您可以使用2个捕获组,首先匹配^[message] ,然后匹配开始捕获组。

\\^\\[[^\\]]+]\\s+\\[([^\\]]+)\\]\\(([^)]+)\\)

Regex demo 正则表达式演示

Explanation 说明

  • \\^ match ^ \\^匹配^
  • \\[[^\\]]+] Match what is between [] using a negated character class \\[[^\\]]+] []使用否定的字符类匹配[]之间的内容
  • \\s+ Match one or more times a whitespace character \\s+匹配一个或多个空白字符
  • \\[([^\\]]+)\\] Match what is between [] in a capturing group using a negated character class (group 1) \\[([^\\]]+)\\]使用否定的字符类(组1)匹配捕获组中[]之间的内容
  • \\(([^)]+)\\) Match what is between () in a capturing group using a negated character class (group 2) \\(([^)]+)\\)匹配是什么之间()中使用否定的字符类捕获组(组2)

 const regex = /\\^\\[[^\\]]+]\\s+\\[([^\\]]+)\\]\\(([^)]+)\\)/; const str = `^[message] [site](http://example.com)`; let [, group1, group2] = str.match(regex); console.log(group1); console.log(group2); 

The problem in your regex is : \\[(.*?)\\] it is matching everything in the outer square bracket [] , to fix this you can use [^[]* instead of (.*?) , because the bigger outer square bracket [] contains [ , therefore including ^\\[ will exclude it from matching. 正则表达式中的问题是: \\[(.*?)\\]匹配外部方括号[] ,要解决此问题,您可以使用[^[]*代替(.*?) ,因为更大外方括号[]包含[ ,因此包含^\\[会将其从匹配项中排除。

/\[([^\[]*)\]\((.*?)\)/gm

try demo at regex 101 在正则表达式101上尝试演示

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

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