简体   繁体   中英

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

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

 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;

// 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.

Instead you could use 2 capturing groups first matching ^[message] and after that match start the capturing groups.

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

Regex demo

Explanation

  • \\^ match ^
  • \\[[^\\]]+] Match what is between [] using a negated character class
  • \\s+ Match one or more times a whitespace character
  • \\[([^\\]]+)\\] Match what is between [] in a capturing group using a negated character class (group 1)
  • \\(([^)]+)\\) Match what is between () in a capturing group using a negated character class (group 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

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