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

Time:08-13

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"

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.

CodePudding user response:

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

  • Related