JSFiddle - React, Tailwind, and code Playground

HTML

<input type="text" id="tf"> 
    <button type="button" class="wrap">wrap</button>

JavaScript

var start, end;
$('#tf').select(function(e) {
    start = e.target.selectionStart;
    end = e.target.selectionEnd;
});

$('.wrap').click(function() {
    var val = $('#tf').val();
    var newVal = val.substr(0, start) + '!' + val.substr(start, (end-start)) + '!' + val.substr(end);
    $('#tf').val(newVal);
});