• Node.js Video Tutorials

Node.js - doesNotMatch() 函式



Node.js 的 `assert.doesNotMatch()` 函式預期作為引數傳入的 *字串* 不匹配作為第二個引數傳入的正則表示式。

這是 Node.js 的 `assert` 模組的內建函式。

語法

以下是 **Node.js `assert.doesNotMatch()` 函式** 的語法:

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

引數

此函式接受三個引數。具體描述如下。

  • **字串** - (必填) 此引數僅接受字串輸入,否則將向輸出丟擲 AssertionError。

  • **正則表示式** - (必填) 在此引數中,需要傳入一個正則表示式,該表示式不應與 **字串** 匹配。如果兩個輸入(字串和正則表示式)相同,則會向輸出丟擲 AssertionError。

  • **訊息** - (可選) 可以將字串或 Error 型別作為輸入傳遞到此引數。

返回值

當兩個輸入(字串和正則表示式)相同時,此方法將向輸出丟擲 AssertionError。

示例

在下面的示例中,我們將 **字串** 和 **正則表示式** 值以及 **訊息** 傳遞給 Node.js `assert.doesNotMatch()` 函式。

const assert = require('assert');

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

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

輸出

當我們編譯並執行程式碼時,該函式不會向輸出丟擲任何 AssertionError,因為字串和正則表示式不相等。

//  Returns nothing

示例

在下面的示例中,我們將 **字串** 和 **正則表示式** 值以及 **訊息** 傳遞給 *Node.js `assert.doesNotMatch()`* 函式。但是我們傳遞給 **字串** 的值是一個整數。

const assert = require('assert');

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

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

輸出

當我們編譯並執行程式碼時,該函式將丟擲 *AssertionError* 以及 *訊息* 到輸出,因為 **字串** 輸入值必須只能是 *字串*。

/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)

示例

在下面的示例中,我們將 **字串** 和 **正則表示式** 傳遞給 *Node.js `assert.doesNotMatch()`* 函式進行比較。但是我們沒有在 *訊息* 引數中傳遞任何文字。

const assert = require('assert');

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

assert.doesNotMatch(str, reg);

輸出

當我們編譯並執行程式碼時,該函式將分配一個預設錯誤訊息。

/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)

示例

在下面的示例中,我們將 **字串** 和 **正則表示式** 值以及 **訊息** 傳遞給 *Node.js `assert.doesNotMatch()`* 函式。

const assert = require('assert');

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

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

輸出

當我們編譯並執行程式碼時,該函式將向輸出丟擲 *AssertionError*,因為 *字串* 和 *正則表示式* 相同。

/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)

示例

在下面的示例中,我們將 **字串** 和 **正則表示式** 值以及 **訊息** 傳遞給 *Node.js `assert.doesNotMatch()`* 函式。

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');

輸出

當我們編譯並執行程式碼時,該函式將向輸出丟擲 *AssertionError*,因為 *字串* 和 *正則表示式* 部分相同。

/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
廣告
© . All rights reserved.