-1

I am using text.split(' ') to split text by 'space'.

Example: Hi my name is John to ['Hi', 'my', 'name', 'is', 'John'];

I would like to ignore spaces in question mark.

Hi pls 'DO NOT SPLIT THIS'

to

['Hi', 'pls', 'DO NOT SPLIT THIS']

How can I do this?

Thank you for any help!

favok20149
  • 489
  • 2
  • 5
  • 6
  • Why downvote? Why? – favok20149 Feb 03 '20 at 21:14
  • using regular expressions ... depends on what is the exact placeholder(s) for you to NOT split it. The other way is to split first based on your placeholder, and the array left to split it based on whitespace (space). – azbarcea Feb 03 '20 at 21:16
  • see [ask]. We expect a little effort showing what you've tried to solve your issue, then we this code shared with us, people who knows a solution will guide you towards a fix and a solution. Because right now, it looks like a demand to do some work for you. Also, it seems that SO already have an answer for your question \/ – Calvin Nunes Feb 03 '20 at 21:16
  • 1
    Does this answer your question? [Javascript split by spaces but not those in quotes](https://stackoverflow.com/a/25663729/2444210) – IceMetalPunk Feb 03 '20 at 21:17

1 Answers1

0

How about the following?

regex = /\s+(?=(?:[^\'"]*[\'"][^\'"]*[\'"])*[^\'"]*$)/g

"Hi pls 'DO NOT SPLIT THIS'".split(regex)
// [ 'Hi', 'pls', "'DO NOT SPLIT THIS'" ]
Greg
  • 1,845
  • 2
  • 16
  • 26