0

I'm getting started with Node.js and trying to learn it, coming from PHP environment.

I have following RegExp: /([A-Z]{2,})+/gim (two or more letters next to each other).
I have following string: "That's my testing sample but it doesn't work."

So I throw this into Node.js (keep in mind I'm a newbie):

var fs = require("fs");
var request = require("request");
// COMMENTS
var regex = new RegExp(/([A-Z]{2,})+/gim);

//COMMENTS

var thisyear = regex.exec("That's my testing sample but it doesn't work.");

console.log(thisyear);

This is the file in it's entirety.

The output that it returns:

[ 'That',
  'That',
  index: 0,
  input: 'That\'s my testing sample but it doesn\'t work.' ]

The output according to pretty much every site I tested it on:

That
my
testing
sample
but
it
doesn
work

How do I get each separate result in an array of sorts?

P.S.: match() and test() are "not a function".

J. Doe
  • 5
  • 2
  • 1
    You dont need to wrap a regex literal into a regex constructor – Jonas Wilms Jan 09 '18 at 18:47
  • @JonasW. At this point I gave up, regardless whether it's within the constructor or loose it still produces same result. – J. Doe Jan 09 '18 at 18:49
  • 1
    You have to call `.exec()` multiple times to get all the results. It will advance the `index` property each time you call it to get the next set of results. Example here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Examples – jfriend00 Jan 09 '18 at 18:53
  • You realize that `match` and `test` are part of the String, not of the regex? [Try it!](https://tio.run/##Tc6xDoJADAbgnadoWISokLgSBmMc0UEnjcMJBU6B02tBifHZ8SBo3Jq/X/72IhpBsZY3nlcqwa5rhIaUIASN91pqdOyUbDew@ryPkPh/OUa98H1YbaNovdnvRp3h01jfOS7nh9NrMXu7Uz@TZWAZ@6OD5VxSi2YIwd7ngicEZQtsmmWVAYnyViCcawbJkCikasLwUPrq2V4pOM6d4Zh5wopVRapAr1CZ8211g677AA) – Jonas Wilms Jan 09 '18 at 18:53
  • @jfriend00 That's weird, but it's also the solution. Thanks. – J. Doe Jan 09 '18 at 18:54

1 Answers1

1

To get multiple results with the g flag, you call .exec() multiple times like this:

let regex = /([A-Z]{2,})+/gim;
let str = "That's my testing sample but it doesn't work.";
let results;

while ((results = regex.exec(str)) !== null) {
    console.log(results[0]);
}

Javascript will set the .index property on the regex object to keep track of where it is searching in the source string and each time you call it, it will return the next set of results.

Note: When using the literal form of a regex /something/, you do not put it inside a new RegExp() constructor. The language makes you a regex object automatically when using the literal syntax.


FYI, you can get all the matches without using a while look like this:

let re = /([A-Z]{2,})+/gim;
let str = "That's my testing sample but it doesn't work.";
console.log(str.match(re));

This is generally the simpler way to do it unless you need to get the groups from your regex. If you need to groups rather than just the whole match, then you have to use the .exec() form to get multiple matches, each with multiple groups.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @J.Doe - I added another option using `.match()` that is simpler if you aren't trying to see capture groups. – jfriend00 Jan 10 '18 at 01:32