3

There are a lot of questions on SO re: matching within a brackets, parens, etc but I'm wondering how to match within a character (parens) while ignoring that character

"Key (email)=(basil@gmail.com) already exists".match(/\(([^)]+)\)/g)
// => ["(email)", "(basil@gmail.com)"]

I believe this is because JS doesn't support [^)]. Given that, how would you recommend extracting the values within the parens.

I'd prefer something less hacky than having to call .replace('(', '').replace(')', '') on the values

I'd like the return value to be ["email", "basil@gmail.com"]

bsiddiqui
  • 1,886
  • 5
  • 25
  • 35

4 Answers4

4

Extract text between braces () not returning braces

using this two banally simple examples:

Using .match() to extract and construct the Array

var arr = "Key (email)=(basil@gmail.com) already exists".match(/(?<=\()[^)]+(?=\))/g);

console.log( arr );

https://regex101.com/r/gjDhSC/1
MDN - String.prototype.match()


Using .replace() to extract values and push to Array

var arr=[];

"Key (email)=(basil@gmail.com) already exists"
    .replace(/\(([^)]+)\)/g, (p1, p2) => arr.push(p2) );

console.log( arr );

https://regex101.com/r/gjDhSC/2
MDN - String.replace()

Community
  • 1
  • 1
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
2

See here: How do you access the matched groups in a JavaScript regular expression?

Combine with @pherris' comment:

var str = "Key (email)=(basil@gmail.com) already exists";
var rx = /\((.+?)\)/ig;
match = rx.exec(str);
alert(match[1]);
while (match !== null) {
   match = rx.exec(str);
   alert(match[1]);
}

JSfiddle: https://jsfiddle.net/60p9zg89/ (will generate popups)


This executes the regex multiple times, returning the captured string (in the brackets).

Pushing an alert to the browser isn't very useful, you specified returning an array - so:

function rxGroups(rx, str) {
   var matches = new Array();
   match = rx.exec(str);
   while (match != null) {
      matches.push(match[1]);
      match = rx.exec(str);
   }
   return matches;
}

var x = rxGroups(
   /\((.+?)\)/ig,
   "Key (email)=(basil@gmail.com) already exists"
);

alert(x.join('\n'));

https://jsfiddle.net/60p9zg89/1/

Community
  • 1
  • 1
wally
  • 3,492
  • 25
  • 31
0

You may be wanting the result of the first capture group. After your match you can get the first group:

(function(s, x) {
 return s.match(x)
    .map(function(m) {
      var f = m.match(x);
      return RegExp.$1;
    });
})('Key (email)=(basil@gmail.com) already exists', /\(([^()]+)\)/g)

Result = ["email", "basil@gmail.com"]

Kris Oye
  • 1,158
  • 14
  • 27
  • Don't know why you down voted this. It's a widely accepted answer: http://stackoverflow.com/questions/3003310/how-can-i-match-on-but-exclude-a-regex-pattern ... good luck to you. – Kris Oye Nov 11 '15 at 23:58
  • agreed - +1 from me, it works. Arguably cleaner than my solution too (performance of the anonymous function and .map is pretty efficient I'd assume?) – wally Nov 12 '15 at 00:25
  • I don't know how performant the code is, actually. It seems like there should be a better way. – Kris Oye Nov 12 '15 at 00:27
  • And despite almost every browser supporting it, Perl-style $1..$9 are not officially part of ecma. – Kris Oye Nov 12 '15 at 00:29
  • 1
    One would hope they continue to standardise such shortcuts/improvements for regex in it though - with more power in the local browsers (as we move yet again away from thin client/fat servers and into fat client / thin servers) there is a lot of obvious justification for it. – wally Nov 12 '15 at 00:36
  • I'm thinking the poster is gaming this post for rep anyway and I'm pretty sure the question is a dupe to boot. – Kris Oye Nov 12 '15 at 01:16
  • @KrisOye I wasn't the one who downvoted and think asking a question for rep is ridiculous. You're right, people have asked this question but in the time I spent researching, couldn't find one that worked and had the result I wanted without parens. I appreciate your answer but accepted a different answer – bsiddiqui Nov 12 '15 at 05:14
0
var str = "Key (email)=(basil@gmail.com) already exists";
var matches = [];
str.replace(/\((.*?)\)/g, function(g0,g1){matches.push(g1);})
cpugourou
  • 775
  • 7
  • 11