JSFiddle - React, Tailwind, and code Playground

by Alexandre Simoes

JavaScript

var str = '"free text" new string "search again"';

var result = str.replace(/([^"]+)|("[^"]+")/g, function($0, $1, $2) {
    if ($1) {
        return $1.replace(/\s/g, '+');
    } else {
        return $2; 
    } 
});

console.log(result);