I am looking for a way to replace the quotes with “corrected” quotations marks in an user input.
The idea
Here is a snippet briefly showing the
So instead of following a regex replace approach, I would use a simple looping with a quotes balancing act. You assume the every single quote that appears will match with another one and when it does it will be replaced as pairs.
Below is a test implementation for the same
String.prototype.replaceAt=function(index, replacement) {
return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
}
tests =[
// [`I'm "happy"! J'ai enfin "osé". La rencontre de mon "âme-sœur" a "été" au rendez-vous…
// and how it should look after correction:`, `I'm "happy"! J'ai enfin "osé". La rencontre de mon "âme-sœur" a "été" au rendez-vous…
// and how it should look after correction:`],
[`tarun" lalwani"`, `tarun” lalwani”`],
[`tarun lalwani"`, `tarun lalwani”`],
[`"tarun lalwani`,`“tarun lalwani`],
[`"tarun" lalwani`,`“tarun” lalwani`],
[`"tarun" lalwani"`,`“tarun” lalwani”`],
[`"tarun lalwani"`, `“tarun lalwani”`]
]
function isCharacterSeparator(value) {
return /“, /.test(value)
}
for ([data, output] of tests) {
let qt = "“”"
let qtL = '“'
let qtR = '”'
let bal = 0
let pattern = /["“”]/g
let data_new = data
while (match = pattern.exec(data)) {
if (bal == 0) {
if (match.index == 0) {
data_new = data_new.replaceAt(match.index, qt[bal]);
bal = 1
} else {
if (isCharacterSeparator(data_new[match.index-1])) {
data_new = data_new.replaceAt(match.index, qtL);
} else {
data_new = data_new.replaceAt(match.index, qtR);
}
}
} else {
if (match.index == data.length - 1) {
data_new = data_new.replaceAt(match.index, qtR);
} else if (isCharacterSeparator(data_new[match.index-1])) {
if (isCharacterSeparator(data_new[match.index+1])) {
//previous is separator as well as next one too
// "tarun " lalwani"
// take a call what needs to be done here?
} else {
data_new = data_new.replaceAt(match.index, qtL);
}
} else {
if (isCharacterSeparator(data_new[match.index+1])) {
data_new = data_new.replaceAt(match.index, qtL);
} else {
data_new = data_new.replaceAt(match.index, qtR);
}
}
}
}
console.log(data_new)
if (data_new != output) {
console.log(`Failed to parse '${data}' Actual='${data_new}' Expected='${output}'`)
} ;
}
Update-1: 20-Apr-2018
I have updated the function. There still may be some edge cases, but you should put everything in the test and run it and fix the ones that don't behave as expected