Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to select a word , word start with and including next word

here is the strings:

stop
stop random some company
stopping non stop
arif

I am trying to match stop, stop random, stopping for this I try:

/stop|(stop.\w+)/

in this, both this part as stop and (stop.\w+) works separately. but not works combined. any idea?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


Sample Code:

const data = [
  "stop",
  "stop random some company",
  "stopping non stop",
  "arif"
]

const regex = /stop|(stop.\w+)/

data.forEach((word) => {
  const match = word.match(regex)
  if (match) {
    console.log(match[0])
  }
})

thanks in advance

>Solution :

You can use this regex:

  • Start check with stop
  • If there is a space, check till next word break
  • If there is no space, check till next space
  • Also check if the word is the last word.
const data = [
  "stop",
  "stop random some company",
  "stopping non stop",
  "arif"
]

const regex = /stop([^ ]+| [^ ]+|$)/

data.forEach((word) => {
  const match = word.match(regex)
  if (match){
    console.log(match[0])
  }
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading