-3

I'm not even looking for a solution at this point, just curious why my regular expression works in Firefox, Chrome and regex testers but not in Node 8.12, 10.11 and 10.12.

Here it is:

var str = `import {User} from '..';
import {UserRole} from '..';

export class ServerUser {
  _id?: string;
  id?: string;
  userRole?: string;
  firstName?: string;
  lastName?: string;
  description?: string;
  email?: string;
  telephone?: string;
  profileImg?: string;
  dateOfBirth?: number;
  registeredAt?: number;
  isMale?: boolean;
  password?: string;
  username?: string;
  media?: any[];

  /**
   * call this method to map the User to ServerUser
   *
   * @param {} user
   * @returns {ServerUser}
   */
  static map(user: User): ServerUser {
    const u = {} as ServerUser;
    [...etc.]
`;

var resultArray = str.match(/\s*[_u]id\?\:\sstring\;\s+([.\w\?\:\,\;\[\]\{\}\s]*)\;\n/m);

console.log(resultArray);
document.write(JSON.stringify(resultArray, null, 4));

In node the same regex used like this:

fs.readFile(serverPath, 'utf8', (err1, serverContent) => {
                    if (err1) return resolve(false);
                    const serverPropArray = serverContent.match(/\s*[_u]id\?\:\sstring\;\s+([.\w\?\:\,\;\[\]\{\}\s]*)\;\n/m)[1].split(';').map( el => el.trim() );

Throws an error:

...\gulpfile.js:383
                        const serverPropArray = serverContent.match(/\s*[_u]id\?\:\sstring\;\s+[.\w\?\:\,\;\[\]\{\}\s]*\;\n/m)[1].split(';').map( el => el.trim() );
                                                                                                                              ^

TypeError: Cannot read property '1' of null
    at fs.readFile (C:\wink\winkular-cli\gulpfile.js:383:127)
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

Anyone?

  • 2
    @Pawel - What do you mean by it not working? Certainly it seems to work other than, of course, that `document.write` isn't supported in Node.js. What specifically isn't working? What result are you expecting, and what are you getting instead? – T.J. Crowder Oct 16 '18 at 10:40
  • 1
    @T.J.Crowder Upon re-reading the question I realise `str` is not just the first line. my fault. – phuzi Oct 16 '18 at 10:41
  • @T.J.Crowder Hi! It works in browsers but not in Node.js. Sorry about the lack of info about how I'm using the regex in node - just added it at the end of the post. In node I'm expecting it to return an array containing 2 strings, like it does in browsers. Instead it throws an error as if the regex didn't find any matches. ps. I have confirmed that serverContent !== undefined when running the match on it. – Paweł Krzemiński Oct 16 '18 at 10:56
  • 1
    So, what's the value of `serverContent`? It's hard to say why your regex doesn't match some unknown string. – VLAZ Oct 16 '18 at 10:57
  • Post code, markup, errors, and other textual information **as text**, not as a *picture* of text. Why: http://meta.stackoverflow.com/q/285551/157247 – T.J. Crowder Oct 16 '18 at 10:58
  • @T.J.Crowder fixed (sorry about that - first time posting). – Paweł Krzemiński Oct 16 '18 at 11:05
  • @vlaz the value of serverContent is the content of a .ts file (angular) i copied its beginning into the snippet at the top of my post and assigned it to the str variable – Paweł Krzemiński Oct 16 '18 at 11:06
  • @PawełKrzemiński - The JavaScript engine in Node.js and Chrome is the same, so if you're getting different results, the reason is that the strings are different. You'll want to [debug the problem](https://nodejs.org/en/docs/guides/debugging-getting-started/) to find out why. – T.J. Crowder Oct 16 '18 at 11:07
  • @PawełKrzemiński are you sure that's the content? Because I'm not. The fact that you get an error indicates it. I've also tried your first snippet in a Node REPL and I am getting the match. So I'd suggest you log the content or debug and make sure `serverContent` really is what you expect it to be. – VLAZ Oct 16 '18 at 11:09
  • Thank you all! The solution provided by @barbsan worked for me – Paweł Krzemiński Oct 16 '18 at 12:08

1 Answers1

0

The reason is newline character in your regex: \n.
Apparently you don't run node on Unix system, so you can:

or:

  • get rid of it

or (better):

  • satisfy all of them, using (\r|\n|\r\n)
barbsan
  • 3,418
  • 11
  • 21
  • 28