parsings strings: extracting words and phrases [JavaScript]

前端 未结 10 688
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 19:20

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 suffic

10条回答
  •  误落风尘
    2020-12-01 19:45

    Thanks a lot for the quick responses!

    Here's a summary of the options, for posterity:

    var input = 'foo bar "lorem ipsum" baz';
    
    output = input.match(/("[^"]+"|[^"\s]+)/g);
    output = input.match(/"[^"]*"|\w+/g);
    output = input.match(/("[^"]*")|([^\s"]+)/g)
    output = /(".+?"|\w+)/g.exec(input);
    output = /"(.+?)"|(\w+)/g.exec(input);
    

    For the record, here's the abomination I had come up with:

    var input = 'foo bar "lorem ipsum" "dolor sit amet" baz';
    var terms = input.split(" ");
    
    var items = [];
    var buffer = [];
    for(var i = 0; i < terms.length; i++) {
        if(terms[i].indexOf('"') != -1) { // outer phrase fragment -- N.B.: assumes quote is either first or last character
            if(buffer.length === 0) { // beginning of phrase
                //console.log("start:", terms[i]);
                buffer.push(terms[i].substr(1));
            } else { // end of phrase
                //console.log("end:", terms[i]);
                buffer.push(terms[i].substr(0, terms[i].length - 1));
                items.push(buffer.join(" "));
                buffer = [];
            }
        } else if(buffer.length != 0) { // inner phrase fragment
            //console.log("cont'd:", terms[i]);
            buffer.push(terms[i]);
        } else { // individual term
            //console.log("standalone:", terms[i]);
            items.push(terms[i]);
        }
        //console.log(items, "\n", buffer);
    }
    items = items.concat(buffer);
    
    //console.log(items);
    

提交回复
热议问题