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 principle:
For quotes, the “correct” ones have an opening “
and a closing ”
, so it needs to be replaced in the good way.
$('#myInput').on("keyup", function(e) {
// The below doesn't work when there's no space before or after.
this.value = this.value.replace(/ "/g, ' “');
this.value = this.value.replace(/" /g, '” ');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="myInput"></textarea>
But the above is not working in all cases.
For example, when the "quoted word" is at the very beginning or the very end of a sentence or a line.
Examples
Possible inputs (beware, french inside! :)) :
⋅ I'm "happy" ! Ça y est, j'ai "osé", et mon "âme sœur" était au rendez-vous…
⋅ The sign says: "Some text "some text" some text." and "Note the space here !"
⋅ "Inc"or"rect" quo"tes should " not be replaced.
⋅ I said: "If it works on 'singles' too, I'd love it even more!"
Correct outputs:
⋅ I'm “happy” ! Ça y est, j'ai “osé”, et mon “âme sœur” était au rendez-vous…
⋅ The sign says: “Some text “some text” some text.” and “Note the space here !”
⋅ “Inc"or"rect” quo"tes should " not be replaced.
⋅ I said: “If it works on ‘singles’ too, I'd love it even more!”
Incorrect outputs:
⋅ The sign says: “Some text ”some text“ some text.” and […]
Why it is incorrect:
→ There should be no space between the end of a quotation and its closing mark.
→ There should be a space between a closing quotation mark and a word.
→ There should be a space between a word and an opening quotation mark.
→ There should be no space between an opening quotation mark and its quotation.
The need
How could it be possible to effectively and easily replace the quotes in all those cases?
If possible, I'd also like the solution to be able to "correct" the quotes even if we add them after the typing of the whole sentence.
Note that I don't (can't) use the word delimiter "\b" in a regex because the “accented characters, such as "é" or "ü" are, unfortunately, treated as word breaks.” (source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
Of course, if there is no other solution, I'll come up with a list of what I consider a word delimiter and use it in a regex. But I'd prefer to have a nice working function rather than a list!
Any idea would be appreciated.