If a string contains a single quote "
, I need to replace it with double quotes ""
. However, sometimes a valid double-quote can be followed by a single quote, eg. """
, which just needs another quote added to the end. If I use a standard replace, eg. replace('"', '""')
, all the quotes are turned into doubles, of course, not just the odd one.
What I need is to find any odd number of consecutive quotes (including a single one on its own) and simply add another quote onto the end. Eg. "
bees ""
, and """
bees """"
.
Is there a regex replace in JavaScript which can acplish this?
If a string contains a single quote "
, I need to replace it with double quotes ""
. However, sometimes a valid double-quote can be followed by a single quote, eg. """
, which just needs another quote added to the end. If I use a standard replace, eg. replace('"', '""')
, all the quotes are turned into doubles, of course, not just the odd one.
What I need is to find any odd number of consecutive quotes (including a single one on its own) and simply add another quote onto the end. Eg. "
bees ""
, and """
bees """"
.
Is there a regex replace in JavaScript which can acplish this?
Share Improve this question asked Jan 30, 2014 at 2:53 ingredient_15939ingredient_15939 3,1447 gold badges38 silver badges56 bronze badges 3-
I've e up with the following, which uses look-behind/ahead, but JavaScript doesn't seem to support it:
((?<!")(?:"")*"(?!"))
replace with$1"
regex101./r/mX9xI7 – ingredient_15939 Commented Jan 30, 2014 at 3:27 - This is a nice programming problem and it's pretty tricky to get right, so +1. However, this task is precisely what one would do to escape double quotes when writing CSV. If so, you'd want to use an existing CSV library rather that rolling this on your own, I would think. – Ray Toal Commented Jan 30, 2014 at 3:28
- Correct, JavaScript does not support negative lookbehind. – Ray Toal Commented Jan 30, 2014 at 3:29
3 Answers
Reset to default 6Are the quotes consecutive? Unless I've misunderstood your requirement, this would work...
str = str.replace(/\"\"?/g, '""')
Explanation: Matches a single quote, optionally followed by another quote, and replaces one/both with two quotes.
Example: http://jsfiddle/aR6p2/
Or alternatively, if it's just a matter of appending a quote when there's an odd number of quotes in a string...
var count = str.split('"').length - 1
str = str + (count % 2 == 0 ? '' : '"')
You can just do this:
var str = '"" """" """ """""';
var re = /([^"]|^)"("")*(?!")/g;
console.log(str.replace(re, '$1(quotes)')); // '"" """" (quotes) (quotes)'
What that does is the following:
- it matches a non-quote - or the start of the entered string - first, and stores it in the first capturing group
- then it matches one double-quote
- then a group of 2 double-quotes, for any amount of times (0 or more)
- it then checks if the next character is a non-quote, but without actually matching it.
- This is then replaced by the value captured by the first capturing group (the non-quote), and the string
(quotes)
.
Basically, it just replaces any odd amount of double-quotes with (quotes)
.
Demo
There's probably a crazy regular expression that will do that, but to keep myself from going crazy I'd do this.
str = str.replace(/[^"]("+)/, function(match, group1){
if((group1.length % 2) === 1 || group1.length === 1){
return group1+'"';
}
return group1;
});