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

javascript, handle string with quoted and non quoted (like search string)

I am sure this has been answered but have not been able to find exactly what I am looking for.

I have a string

"Last Winter" tournament "final score"

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

I want to be able to return a list

["Last Winter", "tournament", "final score"]

What is the correct regex for this, I have been able to pull out various parts, but just not the exact thing.

>Solution :

const str = '"Last Winter" tournament "final score"';
const regex = /("[^"]+")|([^\s]+)/g;
const result = str
  .match(regex)
  .map(s => s.replace(/"([^"]+)"/, '$1'))
console.log(result);
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