JavaScript Split string by all spaces except those in parentheses split string spaces java,split string spaces python,split string

I'm trying to split text the following like on spaces:

var line = "Text (what is)|what's a story|fable called|named|about {Search}|{Title}"

but I want it to ignore the spaces within parentheses. This should produce an array with:

var words = ["Text", "(what is)|what's", "a", "story|fable" "called|named|about", "{Search}|{Title}"];

I know this should involve some sort of regex with line.match(). Bonus points if the regex removes the parentheses. I know that word.replace() would get rid of them in a subsequent step.

Answer:1

Use the following approach with specific regex pattern(based on negative lookahead assertion):

var line = "Text (what is)|what's a story|fable called|named|about {Search}|{Title}",
    words = line.split(/(?!\(.*)\s(?![^(]*?\))/g);
  
console.log(words);
Answer:2

Not a single regexp but does the job. Removes the parentheses and splits the text by spaces.

var words = line.replace(/[\(\)]/g,'').split(" ");
Answer:3

I have a template Hello, ${user.name} stored in a variable. I am reading this from an external file using fs.read. Now, obviously when I attach to the innerHTML of a target div, it shows the string ...

I have a template Hello, ${user.name} stored in a variable. I am reading this from an external file using fs.read. Now, obviously when I attach to the innerHTML of a target div, it shows the string ...

I have a search form(search form) rendered inside a modal window(angular ui bootstrap modal). The input fields hold values that update my ng-model on submit. <script type="text/ng-template" id="...

I have a search form(search form) rendered inside a modal window(angular ui bootstrap modal). The input fields hold values that update my ng-model on submit. <script type="text/ng-template" id="...

  1. angular canactivate
  2. angular can't resolve all parameters
  3. angular cannot find module
  4. angular cannot read property of undefined
  5. angular candeactivate
  6. angular cannot get /
  7. angular canvas
  8. angular cancel http request
  9. angular cannot match any routes
  10. angular cannot find control with name
  11. angular canactivate redirect
  12. angular cannot find name 'require'
  13. angular cannot determine the module for class
  14. angular canload
  15. angular cannot access before initialization
  16. angular canactivatechild
  17. angular candeactivate example
  18. angular cannot set property of undefined
  19. angular cannot find control with path
  20. angular cannot read property of null

I have a webpage with a variety of text snippets enclosed in <span class="x"></span> tags. I'd like to generate an ordered list of each such snippet. Straightforward enough. The wrinkle: ...

I have a webpage with a variety of text snippets enclosed in <span class="x"></span> tags. I'd like to generate an ordered list of each such snippet. Straightforward enough. The wrinkle: ...

  1. extract text from image
  2. extract text from pdf
  3. extract text from excel cell
  4. extract text from pdf python
  5. extract text from pdf image
  6. extract text from image python
  7. extract text from html
  8. extract text from cell
  9. extract text from website
  10. extract text from html python
  11. extract text from powerpoint
  12. extract text from image mac
  13. extract text from video
  14. extract text from word document
  15. extract text from image onenote
  16. extract text from string python
  17. extract text from youtube video
  18. extract text from png
  19. extract text from image android
  20. extract text from webpage

I'm trying to make a super simple lightbox for some images. Basically I have a div with a set of images showing up using ng-repeat, and I want to have a lightbox div show up when one of the images is ...

I'm trying to make a super simple lightbox for some images. Basically I have a div with a set of images showing up using ng-repeat, and I want to have a lightbox div show up when one of the images is ...

  1. angular show hide
  2. angular show hide div
  3. angular show hide div on click
  4. angular show hide component
  5. angular show hide element
  6. angular show hide password
  7. angular show hide animation
  8. angular show hide on click
  9. angular show hide element on click
  10. angular show hide button
  11. angular show hide div on button click
  12. angular show hide div on checkbox
  13. angular show hide component on click
  14. angular show hide directive
  15. angular show hide div with animation
  16. angular show hide form control
  17. angular show hide based on variable
  18. angular show hide menu
  19. angular show hide div on click with animation
  20. angular show hide sidebar