Scan JS object for RegEx patterns

Photo by Caspar Camille Rubin on Unsplash

RegEx is a powerful tool. Many people hate it, because it looks confusing, but all it takes is 1 hour of going through a great tutorial to learn RegEx and save a lot of time for the rest of your life.

JavaScript strings come with a method called match , You can use this method to search for a RegEx pattern on a string. But what if you need to check all strings in an object, e.g. a request body? At some point you’ll need a npm package for searching regex on object.

RegEx-Ray does just that, it takes an object, and an array of RegEx patterns, then checks all string properties (nested ones included) for all members of the RegEx array asynchronously! It makes finding string matches on object very easy.

Think of RegEx-Ray as a RegEx X-Ray for JavaScript Objects!

const regexray = require('regexray');const testObj = {
foo: 'string one hello',
bar: {a: {b: 'hello'},c: 'hello there general kenobi'}
};
console.log(await regexray(testObj, [/el/,/ken/]))// this will be the result:
[
{
path: 'foo',
log: [ 'el', index: 12, input: 'string one hello', groups: undefined ]
},
{
path: 'bar.c',
log: [
'el',
index: 1,
input: 'hello there general kenobi',
groups: undefined
]
},
{
path: 'bar.c',
log: [
'ken',
index: 20,
input: 'hello there general kenobi',
groups: undefined
]
},
{
path: 'bar.a.b',
log: [ 'el', index: 1, input: 'hello', groups: undefined ]
}
]

Read the documentation on npm for more.

I like to share my knowledge as a web developer, my favorite topic is Javascript.