Search code examples
javascriptregexdelimiter

Javascript how to split string by multiple delimiters and include them in the result


I'm trying to separate string by multiple delimiters and include them in the result. Considering all consecutive non-whitespace characters as individual words. Example: "I'm working on a new super-project wow. Yay!" becomes "!Yay .wow project-super new a on working I'm" My code sofar:

function test(string){
    console.log(string.split(/([.'\!'+a-zA-Z]+)/g ).reverse().join(' ') );
}

var string ="I'm working on a new super-project wow. Yay!"
test(string)

the output so far is: Yay! wow. project - super new a on working I'm

I'm still getting the wrong result. Any help would be appreciated.


Solution

  • I suggest swapping words with the glued punctuation first (using .replace(/(^|\s)(\w+)([^\w\s]+)(?!\S)/g,"$1$3$2")) and then match all non-whitespace chunks and join with a space:

    function test(string){
        console.log(string.replace(/(^|\s)(\w+)([^\w\s]+)(?!\S)/g,"$1$3$2").match(/\S+/g).reverse().join(' ') );
    }
    //"!Yay .wow project-super new a on working I'm"
    var string ="I'm working on a new super-project wow. Yay!"
    test(string)

    The word-punctuation swapping regex details:

    • (^|\s) - Group 1: start of string or whitespace
    • (\w+) - Group 2: one or more word chars
    • ([^\w\s]+) - Group 3: any 1+ words other than word and whitespace chars
    • (?!\S) - no non-whitespace char is allowed immediately to the right of the current location. Together with (^|\s), these subpatterns form whitespace word boundaries.