You can use String#replace with RegEx
var regex = /^[.,?!'"]+|[.,?!'"]+$/g;
text = text.replace(regex, '');
RegEx Explanation:
^: Starts of the line anchor
$: End of the line anchor
[.,?!'"]+: Match any of the characters in the character class [ and ] that occur one or more times in any order
|: OR condition/alteration in the RegEx
g: Global flag
RegEx101 Live Demo
var regex = /^[.,?!'"]+|[.,?!'"]+$/g; // Note that you don't need `m` flag here
var resultEl = document.getElementById('output');
document.getElementById('myInput').addEventListener('keyup', function() {
resultEl.innerHTML = this.value.replace(regex, '');
}, false);
<input type="text" id="myInput" placeholder="Type something" />
<div id="output">You'll see output here</div>
From OP's comment
How do I adjust the regex so that it removes characters if the variable ends with 's? For example, if the variable is John's, it will become John
The RegEx can be modified a bit to match 's at the end of the string.
The character class can be make lazy/non-greedy and adding 's as optional group at the end of string.
/^[.,?!'"]+|[.,?!'"]*?(?:'s)?$/
Adding ? in [.,?!'"]*? will make the match lazy. (?:) is non-capturing group and adding ? in (?:'s)? will make the 's optional.
RegEx Demo
Or, as @Daniel Cheung said in the comment, you can also use
/^[.,?!'"]+|(?:[.,?!'"]|'s)+$/
RegEx Demo
Update:
To remove spaces \s can be added in the character class. Or String#trim can also be used after `replace.
/^[.,?!'"\s]+|[.,?!'"\s]+$/
^^ ^^
RegEx Demo
As stated in comment by @Casimir et Hippolyte, you can use ^[\W_]+|[\W_]+$ to remove all the special characters at the beginning and end of the string.