當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js util.types.isRegExp()用法及代碼示例

util模塊的util.types.isRegExp()方法主要用於滿足Node.js自己的內部API的需求。用於檢查傳遞的值是否為正則表達式。

用法:

util.types.isRegExp( value )

參數:此方法接受單個參數值,該參數值包含任何值,即任何模塊的實例。


返回值:此方法返回布爾值,即,如果傳遞的值是正則表達式,則為true,否則返回false。

以下示例說明了Node.js中util.types.isRegExp()方法的使用:

範例1:

// Node.js program to demonstrate the    
// util.types.isRegExp() method 
  
// It includes util module 
const util = require('util'); 
  
// Returns false as the passed instance 
// is not regular expression 
console.log(util.types.isRegExp(new Map())); 
  
// Returns true as the passed instance 
// is regular expression 
console.log(util.types.isRegExp(new RegExp('xyz')));

輸出:

false
true

範例2:

// Node.js program to demonstrate the    
// util.types.isRegExp() method 
  
// It includes util module 
const util = require('util'); 
  
// Returns false as the passed instance 
// is not regular expression 
console.log(util.types.isRegExp(new Map())); 
  
// Returns true as the passed instance 
// is regular expression 
console.log(util.types.isRegExp(new RegExp('xyz'))); 
  
// Returns true as the passed instance 
// is regular expression 
console.log(util.types.isRegExp(/abc/)); 
  
// Returns false as the passed instance 
// is not regular expression 
console.log(util.types.isRegExp(new Int32Array())); 
  
// Returns true as the passed instance 
// is regular expression  
console.log(util.types.isRegExp(/ab+c/));

輸出:

false
true
true
false
true

參考: https://nodejs.org/api/util.html#util_util_types_isregexp_value



相關用法


注:本文由純淨天空篩選整理自akshajjuneja9大神的英文原創作品 Node.js | util.types.isRegExp() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。