2

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

jitter
54.7k11 gold badges114 silver badges130 bronze badges
asked Nov 5, 2009 at 12:22
0

3 Answers 3

11

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"
answered Nov 5, 2009 at 12:29

5 Comments

Very clever. I like it. Have tried to break it with no success.
Thanks Tim. I updated a case when it will break. But the OP didn't mention escaped quotes could occur.
Good point. You could work around that like so: \s+(?=((?:\\"|[^"])*(?<!\\)"(?:\\"|[^"])*(?<!\\)")*(?:\\"|[^"])*$) But now that's really fugly.
Yes, but what about strings that have an escaped backslash right before the closing quote: ab "cd ef\\" gh? When escaped characters come into play, it is time to leave regex-land, IMO.
Oh, and b.t.w., JavaScript does not support look behinds, only look aheads.
2

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ドル || ":" })
 );
answered Nov 5, 2009 at 14:34

Comments

2

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);
answered Nov 5, 2009 at 12:35

1 Comment

You'll want to swap [^"] 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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.