Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使空格在替换文本后保持不变?_Javascript_Jquery - Fatal编程技术网

Javascript 如何使空格在替换文本后保持不变?

Javascript 如何使空格在替换文本后保持不变?,javascript,jquery,Javascript,Jquery,我有文本,在选择时我需要替换文本 我的要求是,在替换包含空格的字符后,空格必须保持不变 JavaScript: function getSel() { // obtain the object reference for the textarea> var txtarea = document.getElementById("mytextarea"); // obtain the index of the first selected character

我有文本,在选择时我需要替换文本

我的要求是,在替换包含空格的字符后,空格必须保持不变

JavaScript:

    function getSel() {
    // obtain the object reference for the textarea>
    var txtarea = document.getElementById("mytextarea");
    // obtain the index of the first selected character
    var start = txtarea.selectionStart;
    // obtain the index of the last selected character
    var finish = txtarea.selectionEnd;
    //obtain all Text
    var allText = txtarea.value;

    // obtain the selected text
    var sel = Array(finish - start).join("*");
    //append te text;
    var newText = allText.substring(0, start) + sel + allText.substring(finish, allText.length);
    txtarea.value = newText;

    $('#newpost').offset({ top: 0, left: 0 }).hide();
}

$(document).ready(function () {
    var position;
    $('#newpost').hide();
    $('#mytextarea').on('select', function (e) {
        $('#newpost').offset(position).show();
        var txtarea = document.getElementById("mytextarea");
        var start = txtarea.selectionStart;
        var finish = txtarea.selectionEnd;
        $('#newpost p').text(Array(finish - start).join("*"));
    }).on('mousedown', function (e) {
        position = { top: e.pageY-5, left: e.pageX};
    });
    $('#newpost').hide();
});
这是我的


我获得的输出如上图所示,但在预期的输出中,空格不能替换为星号。

使用
字符串。替换
,请尝试以下操作:


console.log('g2ggggggggg.replace(/[a-zA-Z0-9]/g,'*'))
我不太擅长正则表达式,所以我使用了for循环,但这可能仍然对您有所帮助

$(document).ready(function () {
    $('#mytextarea').on('select', function (e) {
        var $output = $("#output");
        var $txtarea = $("#mytextarea");
        var start = $txtarea[0].selectionStart;
        var finish = $txtarea[0].selectionEnd;
        var subtext = $txtarea.text().substr(start, finish); 
        var out = "";

        for (var i = 0; i < subtext.length; i++) {
            var char = subtext[i];

            if (char == " ") {
                out += " ";
            } else {
                out += "*";
            }
        }

        $output.text(out);     
    });
});
$(文档).ready(函数(){
$('#mytextarea')。在('select',函数(e)上{
变量$output=$(“#output”);
var$txtarea=$(“#mytextarea”);
var start=$txtarea[0]。selectionStart;
var finish=$txtarea[0]。选择结束;
var subtext=$txtarea.text().substr(开始、完成);
var out=“”;
for(var i=0;i

根据您的代码,您可以在中看到工作示例:

这将有助于显示一些示例预期的输入/输出。您的实际问题是什么?请提供一个包含当前结果和预期结果的示例。@Carcigenicat已更新预期输出,请检查是否有JS提琴供不喜欢Plunker(作为我)的人使用@zeropublix不工作如果我输入字母数字范围以外的字符怎么办?你应该改为使用
[^]
来匹配任何不是空格的字符。是的,但我并没有尝试涵盖所有情况,最初的问题非常简单。用户一点也不简单。例如,您的代码甚至不处理标点符号。作者可能能够自己决定他需要什么标准,我给了他一个如何做的要点,而不是解决他的业务。这听起来像是在洗手。当您回答StackOverflow问题时,您不仅有责任帮助OP,而且有责任帮助将来可能有类似问题的任何人和所有人。我从哪里着手解决效率低下的问题?我的意思是一个字符一个字符地构建一个新字符串(任意长度),调用
.substr
来获得一个字符(使用
subtext[I]
——毕竟字符串基本上是字符数组),等等…@NiettheDarkAbsol感谢您的建议。我已经编辑了我的代码