1

I have a few strings that I am trying to match against

  1. Cafe Team Member: Grovedale (Featured)
  2. Cafe Team Member: Barwon Heads (Featured)
  3. Cafe Team Member: Barwon Heads

What I have been able to come up with is /(?<=\:\s)(.+)(?=\s\()/g which will work for options one and two but 3 (because I am looking for an open bracket after the location) it will not work. I am stumped about how else I could go about this.

Where i have been testing

Edit:

Figured all i needed to add was |$ so it now looks like /(?<=\:\s)(.+)(?=\s\(|$)/g but when testing it in codepen it still isn't capturing the string correctly. Where regexr is saying it should work.

// vue stuff
new Vue({
    el: "#app",
    data: {
        titles: [
          "Cafe Team Member: Grovedale (Featured)",
          "Cafe Team Member: Barwon Heads (Featured)",
          "Cafe Team Member: Barwon Heads",
        ]
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <template v-for="title in titles">
    <p>{{ title.match(/(?<=\:\s)(.+)(?=\s\(|$)/g) }}</p>
  </template>
</div>

2 Answers 2

1

Try using this regex:

(?<=:\s)[^(\r\n]+

Click for Demo

Explanation:

  • (?<=:\s) - positive lookbehind to match the position immediately preceded by a : followed by a white-space
  • [^(\r\n]+ - matches 1+ occurrences of any character that is neither a ( nor the newline characters. This will make sure to match all the characters until ( is found in the same line. If ( is not found, it matches till end of line.
Sign up to request clarification or add additional context in comments.

Comments

1

Your regex doesn't match just because .+ must be followed with ?, which match as few times as possible. Without it, (.+) will match until the end of line and your positive lookahead will not match anything.

// vue stuff
new Vue({
    el: "#app",
    data: {
        titles: [
          "Cafe Team Member: Grovedale (Featured)",
          "Cafe Team Member: Barwon Heads (Featured)",
          "Cafe Team Member: Barwon Heads",
        ]
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <template v-for="title in titles">
    <p>{{ title.match(/(?<=\:\s)(.+?)(?=\s\(|$)/g) }}</p>
  </template>
</div>

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.