in Education by
I need to support exact phrases (enclosed in quotes) in an otherwise space-separated list of terms. Thus splitting the respective string by the space-character is not sufficient anymore. Example: input : 'foo bar "lorem ipsum" baz' output: ['foo', 'bar', 'lorem ipsum', 'baz'] I wonder whether this could be achieved with a single RegEx, rather than performing complex parsing or split-and-rejoin operations. Any help would be greatly appreciated! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
var str = 'foo bar "lorem ipsum" baz'; var results = str.match(/("[^"]+"|[^"\s]+)/g); ... returns the array you're looking for. Note, however: Bounding quotes are included, so can be removed with replace(/^"([^"]+)"$/,"$1") on the results. Spaces between the quotes will stay intact. So, if there are three spaces between lorem and ipsum, they'll be in the result. You can fix this by running replace(/\s+/," ") on the results. If there's no closing " after ipsum (i.e. an incorrectly-quoted phrase) you'll end up with: ['foo', 'bar', 'lorem', 'ipsum', 'baz']

Related questions

0 votes
0 votes
    How many reserved words are there in JavaScript? (a) 63 (b) 54 (c) 68 (d) 90 I have ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 22, 2021 in Education by JackTerrance
0 votes
    input: phrase 1, phrase 2 output: semantic similarity value (between 0 and 1), or the probability these ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 31, 2022 in Education by JackTerrance
0 votes
    I am trying to get a thumbnail image from a video from a URL using AVFoundation but I am getting ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    object looks like as below { "id":1, "image":"path/to/image", "employee_data":, } again ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    object looks like as below { "id":1, "image":"path/to/image", "employee_data":, } again ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to scrape the data off the webpage below, using Selenium in Python 3: https://www. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I am trying to scrape the data off the webpage below, using Selenium in Python 3: https://www. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I need to extract frames in certain interval of the video, (eg from 40 sec to 50 sec). Working ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I want to extract an extension from a filename in Python, How can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Which of these is a process of extracting/removing the state of an object from a stream? (a) ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    The _______________ layer is responsible for extracting the features required from the input. (1)Hidden (2)Input (3)Output...
asked Jun 15, 2021 in Technology by JackTerrance
0 votes
    Can you write down a general regular expression for extracting the IP address from logs?...
asked Oct 31, 2020 in Technology by JackTerrance
0 votes
    Recently I asked a question regarding preg_match_all with php, and i got the perfect solution as expected ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
...