How to search a key/value pair with a regex in JavaScript?
<p>I have a JavaScript Object filled with key/value pairs and I would like to search one with a regex. Here's an example:</p>

<pre><code>var map = {
    'my/route/one': 'Title1',
    'my/route/two': 'Title2',
    '/': 'Home'
}
</code></pre>

<p>And I would like something like this:</p>

<p><code>var myValue = searchByKey(map, 'my/route/two');</code></p>

<p>I did this at first:</p>

<pre><code>searchByKey: function (map, routePattern) {
    var foundTitle,
        route;
    for (route in map) {
        if (routePattern.match(route)) {
            foundTitle = map[route];
            break;
        }
    }
    return foundTitle;
}
</code></pre>

<p>It worked, great. Then, I wanted to have something more functional, so I thought of this:</p>

<pre><code>function searchByKey(map, routePattern) {
    var foundTitle;
    Object.keys(map).forEach(function(key) {
        if (routePattern.match(key)) {
            foundTitle = map[key];
        }
    });
    return foundTitle;
}
</code></pre>

<p>But it may match other keys like the last one <code>'/'</code>.</p>

<p>Do you have any idea on how achieve this in an elegant way?</p>

<p>Thanks!</p>