How to use the isRegExp function from util

Find comprehensive JavaScript util.isRegExp code examples handpicked from public code repositorys.

In Node.js, util.isRegExp is a utility function that checks whether a value is a regular expression.

148
149
150
151
152
153
154
155
156
157
  // true
```


<div id="isRegExp" class="anchor"></div>
## util.isRegExp(object)

> 稳定度:0 - 已废弃

如果给定的 'object' 是 `RegExp`,返回 `true`。否则,返回 `false`。
fork icon21
star icon99
watch icon2

1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
  return actual.getTime() === expected.getTime();

// 7.3 If the expected value is a RegExp object, the actual value is
// equivalent if it is also a RegExp object with the same source and
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
} else if (util.isRegExp(actual) && util.isRegExp(expected)) {
  return actual.source === expected.source &&
         actual.global === expected.global &&
         actual.multiline === expected.multiline &&
         actual.lastIndex === expected.lastIndex &&
fork icon0
star icon0
watch icon1

+ 2 other calls in file

How does util.isRegExp work?

In Node.js, util.isRegExp is a utility function that checks whether a given value is a regular expression.

This function returns true if the value is an instance of the RegExp object, and false otherwise. It is a convenient way to determine whether a given value is a regular expression, without having to use the instanceof operator or perform other checks manually.

Here is an example of how to use util.isRegExp:

javascript
const util = require("util"); console.log(util.isRegExp(/hello/)); // true console.log(util.isRegExp("hello")); // false

In this example, we're using util.isRegExp to check whether a value is a regular expression. We call the function with two arguments: the first is a regular expression literal (/hello/), and the second is a string ("hello"). The function returns true for the regular expression, because it is an instance of the RegExp object, and false for the string, because it is not a regular expression.

util.isRegExp is a useful utility function for situations where you need to check whether a value is a regular expression, such as when working with regular expressions returned from external libraries or user input.

Ai Example

1
2
3
4
5
6
7
8
9
10
const util = require("util");

console.log(util.isRegExp(/hello/)); // true
console.log(util.isRegExp("hello")); // false

const myRegex = new RegExp("\\d+");
console.log(util.isRegExp(myRegex)); // true

const myString = "1234";
console.log(util.isRegExp(myString)); // false

In this example, we're using util.isRegExp to check whether different values are regular expressions. We call the function with four arguments: a regular expression literal (/hello/), a string ("hello"), an instance of the RegExp object (myRegex), and a string that contains digits (myString). The first call to util.isRegExp returns true, because the argument /hello/ is a regular expression. The second call to util.isRegExp returns false, because the argument "hello" is not a regular expression. The third call to util.isRegExp returns true, because myRegex is an instance of the RegExp object. We create the myRegex object using the RegExp constructor, passing it the regular expression pattern \\d+, which matches one or more digits. The fourth call to util.isRegExp returns false, because the argument myString is not a regular expression. By using util.isRegExp, you can easily determine whether a given value is a regular expression or not, without having to manually check its type or other properties.