2

Live code

I have an array of strings. Each string represents a path. I need to remove everything before the locale code in this path. I would like this to return a new array of clean paths.

Question: How to write and use arr.filter() to match() then remove all locale's pattern from the original string.

Code:

var thingy = ['thing/all-br/home/gosh-1.png','thing/ar_all/about/100_gosh.png','thing/br-pt/anything/a_noway.jpg'];
var reggy = new RegExp('/[a-z]{2}-[a-z]{2}|[a-z]{2}_[a-z]{2}/g');


var newThing = thingy.filter(function(item){
       return result = item.match(reggy);
    });

In the end, I would like to filter that original array thingy to newThing which the output should looks like:

console.log(newThing);
// ['home/gosh1.png','about/gosh.png','place/1noway.jpg']
4
  • just change return result = item.match(reggy); to return item.match(reggy); Commented Oct 5, 2015 at 5:22
  • still not working :( Commented Oct 5, 2015 at 5:23
  • jsfiddle.net/arunpjohny/tgL8seyk/1 ? - if the regex is proper... haven't validated that part Commented Oct 5, 2015 at 5:23
  • @T.J.Crowder that was a mistake.. I'm sorry. Commented Oct 5, 2015 at 5:24

1 Answer 1

5

If you want to transform the items in the array, filter isn't the right tool; map is the tool you use.

It looks like you just want to drop the middle part of the path:

var thingy = ['home/all-br/gosh1.png', 'about/ar_all/gosh.png', 'place/br-pt/noway.jpg'];
var newThing = thingy.map(function(entry) {
  return entry.replace(/\/[^\/]+/, '');
});
snippet.log(JSON.stringify(newThing));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

That uses /\/[^\/]+/, which matches a slash followed by any sequence of non-slashes, and then usese String#replace to replace that with a blank string.

If you wanted to use capture groups instead to capture the segments you wanted, you'd do much the same thing, just change what you do in the map callback, and have it return the string you want for that entry.

Just as an example of changing things slightly, here's a similar thing that captures the first and last segments and reassembles them without the part of the middle:

var thingy = ['home/all-br/gosh1.png', 'about/ar_all/gosh.png', 'place/br-pt/noway.jpg'];
var newThing = thingy.map(function(entry) {
  var match = entry.match(/^([^\/]+)\/.*\/([^\/]+)$/);
  return match ? match[1] + "/" + match[2] : entry;
});
snippet.log(JSON.stringify(newThing));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Tweak as necessary.

Sign up to request clarification or add additional context in comments.

4 Comments

map is really the right/best way to go, for this case.
@T.J. could you try with var thingy = ['thing/all-br/home/gosh-1.png','thing/ar_all/about/100_gosh.png','thing/br-pt/anything/a_noway.jpg']; as your staring array.
thus removing thing/<locale>/
@MatthewHarwood: Just tweak as required. This is the fundamental way you do it (well, this or use match and reassemble the string you want from capture groups and return that from the map callback).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.