Reputation: 718
I'm trying to pass objects through a function that finds words between two other words.
I'm trying to pass:
the console should log:
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>
Upvotes: 1
Views: 56
Reputation: 517
Your findStringBetween method accepts a single a string and you would like to use it for multiple string. You have two options:
Use your method inside a loop invoking it for every member of cat.
for(let i=0; i< cat.length(); i++)
Use cat[i]
You can refactor your method to accept an array instead of string. In this case you should use the loop inside your method.
Upvotes: 1