-3

I need to iterate over strings that are inside an array, to get a sub-string in each string.

Substrings are between "()"

Something like this..

let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];

//return
//'cat','red','apple'

How I could do that?

3
  • 5
    Show us what code you have already written and where the problem you have comes in. Or are you just asking someone here to write it for you? Commented Sep 5, 2018 at 19:16
  • 1
    Welcome to Stack Overflow! Here, you can learn How to Ask properly before you do so. When asking a question, be sure you are on topic and always try to provide a Minimal, Complete, and Verifiable example when possible. This way, it's more likely volunteers on SO can help you. Commented Sep 5, 2018 at 19:18
  • What have you tried? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… might contain something which would help you. Or maybe regular expressions would be better. Either way, this is not a free write-my-code service. We'll help you, we won't do it for you. Thanks. Commented Sep 5, 2018 at 19:23

1 Answer 1

0

You can do this using substring and lastIndexOf functions.

let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];

myArray.forEach(function(e){
    console.log(e.substring(e.lastIndexOf("(") + 1, e.lastIndexOf(")")))
})

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.