Currently I have a basic regex in javascript for replacing all whitespace in a string with a semi colon. Some of the characters within the string contain quotes. Ideally I would like to replace white space with a semi colon with the exception of whitespace within quotes.
var stringin = "\"james johnson\" joe \"wendy johnson\" tony";
var stringout = stringin.replace(/\s+/g, ":");
alert(stringout);
Thanks Robin
3 Answers 3
Try something like this:
var stringin = "\"james johnson\" joe \"wendy johnson\" tony";
var stringout = stringin.replace(/\s+(?=([^"]*"[^"]*")*[^"]*$)/g, ":");
Note that it will break when there are escaped quotes in your string:
"ab \" cd" ef "gh ij"
5 Comments
\s+(?=((?:\\"|[^"])*(?<!\\)"(?:\\"|[^"])*(?<!\\)")*(?:\\"|[^"])*$)
But now that's really fugly.ab "cd ef\\" gh
? When escaped characters come into play, it is time to leave regex-land, IMO.in javascript, you can easily make fancy replacements with callbacks
var str = '"james \\"bigboy\\" johnson" joe "wendy johnson" tony';
alert(
str.replace(/("(\\.|[^"])*")|\s+/g, function(0,ドル 1ドル) { return 1ドル || ":" })
);
Comments
In this case regexes alone are not the simplest way to do it:
<html><body><script>
var stringin = "\"james \\\"johnson\" joe \"wendy johnson\" tony";
var splitstring = stringin.match (/"(?:\\"|[^"])+"|\S+/g);
var stringout = splitstring.join (":");
alert(stringout);
</script></body></html>
Here the complicated regex containing \\"
is for the case that you want escaped quotes like \"
within the quoted strings to also work. If you don't need that, the fourth line can be simplified to
var splitstring = stringin.match (/"[^"]+"|\S+/g);
1 Comment
[^"]
and \\"
around. Otherwise the backslash which is meant as an escape is "eaten" by the [^"]
. Or add the backslash in your negated character class. Other than that, your way is a more intuitive approach (and in case of escaped characters, a working solution). +1