Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

50
Vistas
Js string.split with regex on space or special character

I need to split a command string either on a space or if a substring is surrounded by double quotes. So an example input would be:

'ADD PRODUCT "This could be any number of words" 232323-fwe4f434-354trcc2'

With that as in input im looking for an output of:

['ADD', 'PRODUCT', '"This could be any number of words"', '232323-fwe4f434-354trcc2']

Ive tried this and it gets me close but still not quite what i need

input:

const str = 'ADD PRODUCT "This could be any number of words" 232323-fwe4f434-354trcc2'

console.log(str.split(/\s*(")\s*/))

output:

[
  'ADD PRODUCT',
  '"',
  'This could be any number of words',
  '"',
  '232323-fwe4f434-354trcc2'
]
7 months ago · Santiago Gelvez
Responde la pregunta
Encuentra empleos remotos