• Node.js Video Tutorials

Node.js - doesNotMatch() Function



The Node.js assert.doesNotMatch() function will expect the string which is passed as a parameter not to match with the regex (regular expression) which will be passed in the second parameter to the function.

This is an inbuilt function of the assert module of Node.js.

Syntax

Following is the syntax of Node.js assert.doesNotMatch() function

assert.doesNotMatch(string, regexp[, message]);

Parameters

This function accepts three parameters. The same are described below.

  • String − (required) This parameter will only accept if the input is a string else it will throw an AssertionError to the output.

  • Regexp − (required) In this parameter, we need to pass a regular expression that should not match the string. If both the inputs (string & regexp) are the same it will throw an AssertionError to the output.

  • Message − (optional) String or Error type can be passed as an input into this parameter.

Return Value

This method will throw an AssertionError to the output when both the inputs (string & regexp) are the same.

Example

In the example below, we passed both string and regex values to the Node.js assert.doesNotMatch() function along with the message.

const assert = require('assert');

var str = 'Tutorialspoint';
var reg = /E-learning platform/;

assert.doesNotMatch(str, reg, 'Both are not matching');

Output

When we compile and run the code, the function will not throw any AssertionError to the output as both string and regex are not the same.

//  Returns nothing

Example

In the example below, we passed both String and regex values to the Node.js assert.doesNotMatch() function along with the message. But the value we are passing to the string is an integer.

const assert = require('assert');

var str = 96864;
var reg = /E-learning platform/;

assert.doesNotMatch(str, reg, 'Integer is passes instead of string');

Output

When we compile and run the code, the function will throw AssertionError along with the message to the output because the String input value must be string only.

/home/cg/root/639c2bf348ea8/main.js:6
   assert.doesNotMatch(str, reg, 'Integer is passes instead of string');
      ^
   
TypeError: assert.doesNotMatch is not a function
   at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:6:8)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Example

In the example below, we passed String and regexp to the Node.js assert.doesNotMatch() function to compare. But we haven't passed any text into the message parameter.

const assert = require('assert');

var str = 436;
var reg = /Number/;

assert.doesNotMatch(str, reg);

Output

When we compile and run the code, the function will assign a default error message.

/home/cg/root/639c2bf348ea8/main.js:6
   assert.doesNotMatch(str, reg);
      ^
   
TypeError: assert.doesNotMatch is not a function
   at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:6:8)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Example

In the example below, we passed both String and regex values to the Node.js assert.doesNotMatch() function along with the message.

const assert = require('assert');

var str = 'Bahubali';
var reg = /Bahubali/;

assert.doesNotMatch(str, reg, 'Both the values are exactly same');

Output

When we compile and run the code, the function will throw an AssertionError to the output as both string and regex are the same.

/home/cg/root/639c2bf348ea8/main.js:6
   assert.doesNotMatch(str, reg, 'Both the values are exactly same');
      ^
   
TypeError: assert.doesNotMatch is not a function
   at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:6:8)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Example

In the example below, we passed both String and regex values to the Node.js assert.doesNotMatch() function along with the message.

const assert = require('assert');

var str = 'Good to see you back officer';
var reg = /see/;

assert.doesNotMatch(str, reg, 'Both the values are partially same');

Output

When we compile and run the code, the function will throw an AssertionError to the output as both string and regex are partially the same.

/home/cg/root/639c2bf348ea8/main.js:6
   assert.doesNotMatch(str, reg, 'Both the values are partially same');
      ^
   
TypeError: assert.doesNotMatch is not a function
   at Object.<anonymous> (/home/cg/root/639c2bf348ea8/main.js:6:8)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
nodejs_assert_module.htm
Advertisements