Javascript regex
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 = stri开发者_StackOverflow社区ngin.replace(/\s+/g, ":");
alert(stringout);
Thanks Robin
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"
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 || ":" })
);
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);
精彩评论