1

I'm trying to pass objects through a function that finds words between two other words.

I'm trying to pass:

  • sentences.para1.cat - to - findStringBetween(parameter1,― , ―)
  • sentences.para2 - to - findStringBetween(―, parameter2, ―)
  • sentences.para3 - to - findStringBetween(―, ―, parameter3)

the console should log:

  • love all the
  • hate
  • never had a

sentences = {
  para1: [
    cat: [
      "I love all the cats",
      "I hate cats",
      "You never had a cat before"
    ],
    dog: [
      "We can get a dog"
    ]
  ],
  para2: [
    "I",
    "You",
    "We"
  ],
  para2: [
    "cat",
    "dog"
  ]
}

function findStringBetween(str, first, last) {
  var r = new RegExp(first + "(.*)" + last)
  ab = str.match(r)
  result = ab[1].trim()
  console.log(result)
}


findStringBetween(parameter1, parameter2, parameter3);
//parameter1 should pass all of sentences.para1.cat
//parameter2 should pass all of sentences.para2
//parameter3 should pass all of sentences.para3
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

8
  • there is no JSON in the code, so it's a bit hard to answer Commented Sep 13, 2017 at 5:40
  • use JSON.stringify() b4 pass? Commented Sep 13, 2017 at 5:42
  • Sorry, I just changed the question… I don't know all the terms to coding language. Commented Sep 13, 2017 at 5:42
  • are you saying that how to convert the array into json and send as parameter? Commented Sep 13, 2017 at 5:44
  • No… sorry… I'm just trying to pass it as it is… I though it was called a JSON… but I just found out it's not Commented Sep 13, 2017 at 5:46

1 Answer 1

1

Your findStringBetween method accepts a single a string and you would like to use it for multiple string. You have two options:

  1. Use your method inside a loop invoking it for every member of cat.

    for(let i=0; i< cat.length(); i++)
        Use cat[i]
    
  2. You can refactor your method to accept an array instead of string. In this case you should use the loop inside your method.

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

Comments

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.