0

Am I missing something in basic node.js regular expression syntax?

It doesn't matter if I start with ^ or end with $, what I missing in the regex syntax to get these outputs:

var Regex = require("regex");
//var Regex = require("regexp"); //performs the same with both 


var regex = new Regex(/(a|b)*abb/);
console.log("The pattern matches: " + regex.test("abb"));   // true

//Match a decimal number in sqaure brackets
var regex1 = new Regex(/\d/);
console.log("The pattern matches: " + regex1.test("9"));

var regex2 = new Regex(/^\d$/);
console.log("The pattern matches: " + regex2.test("9"));

var regex3 = new Regex(/^\d$/);
console.log("The pattern matches: " + regex3.test("9"));


var  regex4 = new Regex(/^\[\d\.\d\]$/);
console.log("The pattern matches: " + regex4.test("[9.9]"));

var  regex5 = new Regex(/^\[[0-9]\.[0-9]\]$/);
console.log("The pattern matches: " +regex5.test("[9.9]"));

output:

C:\nodeapps\CSV IO>node regexTest.js

    The pattern matches: true

    The pattern matches: false

    The pattern matches: false

    The pattern matches: false

    The pattern matches: false

    The pattern matches: false

Am I syntactically not seeing something?

3
  • basic regex don't need you to require("regex") Commented May 11, 2016 at 7:37
  • 1
    Just remove all new Regex( ... ). Use plain regex literals. var regex1 = /\d/; should work. Commented May 11, 2016 at 7:39
  • TIP: Your output would be more useful if it included the regex and the target string as well as the result. Having to scan back up the page to see which test we're looking at is annoying, and makes us that much more likely to lose interest and move on to another question. Commented Jun 10, 2016 at 11:53

1 Answer 1

1

We don't need require("regex"); Just Remove it and use. Default javascript RegExp it will work.

Modfied Code is

var regex = new RegExp(/(a|b)*abb/);
console.log("The pattern matches: " + regex.test("abb"));   // true

//Match a decimal number in sqaure brackets
var regex1 = new RegExp(/\d/);
console.log("The pattern matches: " + regex1.test("9"));

var regex2 = new RegExp(/^\d$/);
console.log("The pattern matches: " + regex2.test("9"));

var regex3 = new RegExp(/^\d$/);
console.log("The pattern matches: " + regex3.test("9"));


var  regex4 = new RegExp(/^\[\d\.\d\]$/);
console.log("The pattern matches: " + regex4.test("[9.9]"));

var  regex5 = new RegExp(/^\[[0-9]\.[0-9]\]$/);
console.log("The pattern matches: " +regex5.test("[9.9]"));
Sign up to request clarification or add additional context in comments.

2 Comments

This might not work as expected in all browsers, as I said, regex literal notation is preferred, especially when the regex pattern is known beforehand (is static).
@WiktorStribiżew It will not affect it's functionality across browsers. And as per your requirement you are running RegEx Tests on Server side, independant of browser at client side. So it will work perfectly in your case.

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.