63

Update: This question is a near duplicate of this

I'm sure the answer to my question is out there, but I couldn't find the words to express it succinctly. I am trying to do the following with JavaScript regex:

var input = "'Warehouse','Local Release','Local Release DA'";
var regex = /'(.*?)'/g;

console.log(input.match(regex));

// Actual:
// ["'Warehouse'", "'Local Release'", "'Local Release DA'"]

// What I'm looking for (without the '):
// ["Warehouse", "Local Release", "Local Release DA"]

Is there a clean way to do this with JavaScript regex? Obviously I could strip out the 's myself, but I'm looking for the correct way to caputre globally matched groupings with regex.

3

8 Answers 8

116

To do this with a regex, you will need to iterate over it with .exec() in order to get multiple matched groups. The g flag with match will only return multiple whole matches, not multiple sub-matches like you wanted. Here's a way to do it with .exec().

var input = "'Warehouse','Local Release','Local Release DA'";
var regex = /'(.*?)'/g;

var matches, output = [];
while (matches = regex.exec(input)) {
    output.push(matches[1]);
}
// result is in output here

Working demo: http://jsfiddle.net/jfriend00/VSczR/


With certain assumptions about what's in the strings, you could also just use this:

var input = "'Warehouse','Local Release','Local Release DA'";
var output = input.replace(/^'|'$/, "").split("','");

Working demo: http://jsfiddle.net/jfriend00/MFNm3/


Note: With modern Javascript engines as of 2021, you can use str.matchAll(regex) and get all matches in one function call.

10
  • Added a non-regex way to also do this.
    – jfriend00
    Commented Nov 11, 2013 at 19:07
  • I applied this loop with this regex /<img[^>]+?src=(?:(?:'([^']*)')|(?:"([^"]*)")|([^\s]*))/i and console don't respond and chrome using 1 full cpu core
    – jscripter
    Commented Jul 9, 2015 at 3:17
  • Oh I forgot the g modifier. Now it's ok
    – jscripter
    Commented Jul 9, 2015 at 3:31
  • 1
    this will fail in jslink.. do this instead while ((matches = regex.exec(input)) !== null){
    – keithics
    Commented May 19, 2016 at 12:08
  • @keithics - What in the world is jslink returning that is not null, but is falsey that would make the original code not work? This is weird. .exec() should return an array or null so the original code should detect no match just fine.
    – jfriend00
    Commented May 19, 2016 at 14:38
12

String.prototype.matchAll is now well supported in modern browsers as well as Node.js. This can be used like so:

const matches = Array.from(myString.matchAll(/myRegEx/g)).map(match => match[1]);

Note that the passed RegExp must have the global flag or an error will be thrown.

Conveniently, this does not throw an error when no matches are found as .matchAll always returns an iterator (vs .match() returning null).


For this specific example:

var input = "'Warehouse','Local Release','Local Release DA'";
var regex = /'(.*?)'/g;

var matches = Array.from(input.matchAll(regex)).map(match => match[1]);
// [ "Warehouse", "Local Release", "Local Release DA" ]
1
9

There is an ECMAScript proposal called String.prototype.matchAll() that would fulfill your needs.

2
5

Not very generic solution since lookbehind isn't supported in Javascript but for given input this regex should work:

m = input.match(/([^',]+)(?=')/g);
//=> ["Warehouse", "Local Release", "Local Release DA"]
0
3

With es2020 you can use matchAll:

var input = "'Warehouse','Local Release','Local Release DA'";
var regex = /'(.*?)'/g;

const match_all = [...input.matchAll(regex)];

If you're using typescript, don't forget to set it in tsconfig.json:

"compilerOptions": {
    "lib": ["es2020.string"]
}
1

Try something like input.replace(regex, "$1") to get the results of your capture group.

4
  • 2
    To get into array - input.replace(regex, "$1").split(',')
    – levi
    Commented Nov 11, 2013 at 18:59
  • OP wants the result in an array.
    – jfriend00
    Commented Nov 11, 2013 at 18:59
  • I like your line of thinking, assuming the content doesn't have an ,s in it.
    – Jondlm
    Commented Nov 11, 2013 at 19:11
  • @Jondlm can do - regex = /'(.*?)'(,?)/g; and input.replace(regex, "$1$2$2").split(',,')
    – levi
    Commented Nov 11, 2013 at 19:41
-1

this regex works but with defined characters...

var input = "'Warehouse','Local Release','Local Release DA'";

var r =/'[\w\s]+'/gi;
console.log(input.match(regex));
1
  • 1
    This leaves the single quote marks in the result which is not what the OP wants.
    – jfriend00
    Commented Nov 11, 2013 at 19:32
-2

EDIT: This won't work in javascript, but it does work in java. Sorry bout that.

yes, it's called a "look ahead" and a "look behind"

(?<=').*?(?=')
  • (?=') looks ahead for the '
  • (?<=') looks behind for the '

test it out here

4
  • in javascript there is no look behind.
    – Techsin
    Commented Nov 11, 2013 at 19:00
  • 1
    So why write it then, knowing it doesn't work in Javascript? Otherwise people would ask with a Java tag. Beggar's belief... Commented Jun 1, 2015 at 19:23
  • There is look behind in JavaScript now Commented Dec 20, 2019 at 9:08
  • Here is an article about how to use lookbehind and lookahed: v8.dev/blog/regexp-lookbehind-assertions
    – ikarasz
    Commented Jun 10, 2020 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.