Over a million developers have joined DZone.


· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Inspired by a client project with thousands of lines of poorly structured, badly written ExtJS code, I wrote a grep implementation to recursively search the contents of Javascript variables, available on github.

This provides a single function “grep”, which can be used directly or added to the global namespace. It recursively searches objects – keys, values, arrays and functions – for values matching a regular expression. It does a breadth-first search, stopping after a number of entries.It returns a stringified versions of the variable name and value, so you can see exactly what you’d need to write to get the desired value. e.g:


var lib = ...contents of grep.js...

testTopLevelArray: Passed
testSubLevelArray: Passed
testInt: Passed
testString: Passed
testRecursion: Passed
testObject: Passed
testSubObject: Passed
testFunction: Passed
testWindow: Passed
testRemoveTopLevel: Passed
testRemoveTopLevel: Passed
testRemoveSecondLevel: Passed
testRemoveSecondLevel: Passed
testKeys: Passed
testValues: Passed


You can call lib.grep directly, or call install to place it in the global namespace:



Since “document” is a global, grep returns values you can use to access the results:

grep("document", "location")

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

grep("document", "location").keys()

["document.location", "document.location.assign", "document.location.replace", "document.location.reload", "document.location.ancestorOrigins", "document.location.origin", "document.location.hash", "document.location.search", "document.location.pathname", "document.location.port"]

grep("document", "location$").keys();

["document.location", "document.defaultView.location", "document.activeElement.ownerDocument.location", "document.head.ownerDocument.location", "document.body.ownerDocument.location", "document.defaultView.top.location", "document.defaultView.window.location", "document.defaultView.document.location", "document.defaultView.Modernizr.geolocation", "document.documentElement.classList.7"]

grep("document", "location$").values()

["https://github.com/garysieling/grep-js/blob/master/search/grep.js", "https://github.com/garysieling/grep-js/blob/master/search/grep.js", "https://github.com/garysieling/grep-js/blob/master/search/grep.js", "https://github.com/garysieling/grep-js/blob/master/search/grep.js", "https://github.com/garysieling/grep-js/blob/master/search/grep.js", "https://github.com/garysieling/grep-js/blob/master/search/grep.js", "https://github.com/garysieling/grep-js/blob/master/search/grep.js", "https://github.com/garysieling/grep-js/blob/master/search/grep.js", "true", "geolocation"]

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Published at DZone with permission of Gary Sieling, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}