I'm currently using a replace script to auto fix both single quotes and double quotes.
However, I cannot find a solution to change a double quote nested anywhere inside another double quotes
So that:
“Here’s some extra text at the beginning “abc” and at the end”
should be
“Here’s some extra text at the beginning ‘abc’ and at the end”
Currently, I can only auto-fix this type if it's beside the other quotes (e.g. ““abc””) using a simple replace script
.replaceAll("““", "“‘") and .replaceAll("””", "’”")
Is it possible to use regex to target any double quotes nested inside another?
Note: It's important that the quotes are curly (“” and ‘’) and not the straight ones ("" and '').
function fixTextarea(textarea) {
textarea.value = textarea.value.replace(" ,", ",")
.replaceAll(" ;", ";")
.replaceAll(" .", ".")
.replaceAll(" ", " ")
.replaceAll(" ", " ")
.replaceAll("“ ", "“")
.replaceAll(" ”", "”")
.replaceAll("““", "“‘")
.replaceAll("””", "’”")
.replaceAll(/(^|[-\u2014\s(\["])'/g, "$1\u2018")
.replaceAll(/'/g, "\u2019")
.replaceAll(/(^|[-\u2014/\[(\u2018\s])"/g, "$1\u201c")
.replaceAll(/"/g, "\u201d")
};
function fixtext() {
let textarea = document.getElementById("textarea1");
textarea.select();
fixTextarea(textarea);
}
window.addEventListener('DOMContentLoaded', function(e) {
var area = document.getElementById("textarea1");
var getCount = function(str, search) {
return str.split(search).length - 1;
};
var replace = function(search, replaceWith) {
if (typeof(search) == "object") {
area.value = area.value.replace(search, replaceWith);
return;
}
if (area.value.indexOf(search) >= 0) {
var start = area.selectionStart;
var end = area.selectionEnd;
var textBefore = area.value.substr(0, end);
var lengthDiff = (replaceWith.length - search.length) * getCount(textBefore, search);
area.value = area.value.replace(search, replaceWith);
area.selectionStart = start + lengthDiff;
area.selectionEnd = end + lengthDiff;
}
};
});
<textarea class="lined" id="textarea1" name="textarea1" spellcheck="true" placeholder="" onpaste="console.log('onpastefromhtml')"></textarea>
<br><br>
<button onclick="fixtext()"> Fixit</button>