Javascript:检查字符串是否包含字符,然后删除字符串的一部分

Javascript: Checking if string contains a character, then removing part of a string?

本文关键字:字符串 删除 然后 一部分 包含 检查 是否 Javascript 字符      更新时间:2023-09-26

本质上,这个函数应该做的是从输入文本中获取单词列表,并从下拉菜单中以客户端选择的间隔(WPM(将其设置在显示器上。

如果在函数中传递的单词包含问号、句点、冒号、分号、感叹号或逗号,则会将其删除,并且所选间隔将加倍。例如,如果单词之间的延迟为 117 毫秒,则为 234 毫秒。

我在确定传递的单词是否包含标点符号并将其删除的部分时遇到了最大的麻烦。

我收到错误:未捕获类型错误:无法读取未定义的属性"indexOf"。

我不确定为什么会发生这种情况,因为list[index++]是一个StringindexOf是 Javascript 中的字符串方法而不是属性。

我也不确定我将如何实施延迟。鉴于我以这种方式使用了setInterval()(并且我只能为此目的使用 setInterval(,我不确定如何让它在显示中设置两次String,同时还包括延迟。

function runDisplay(data, id) {
        var reader = document.getElementById(id);
        var index = 0;
        if (timer) {
            clearInterval(timer);
        }
        if (data.length) {
            timer = setInterval(function() {

            var punctuation = [".", ",", ":", ";", "!", "?"];
            var textSpeed = 117; // default
            for (var j = 0; j < punctuation.length; j++) {
                // remove punctuation if found and double text delay
                // if multiple found, remove only one
                if (!(data[index++].indexOf(punctuation[j]) === -1)) {
                    data[index++] = string.replace(punctuation[j], '');
                // set data[index++] to display twice to double delay?
                }
            } 

            reader.innerHTML = data[index++];
            index = index % data.length;
          }, textSpeed);
        }
    }

我收到错误:未捕获的类型错误:无法读取属性 未定义的"索引"。

我不确定为什么会发生这种情况,因为 list[index++] 是一个字符串并且 indexOf 是 Javascript 中的字符串方法,而不是属性。

首先,方法也是对象的属性。

其次,JS引擎告诉你你在undefined上调用indexOf,所以它不是一个字符串。data[index++]是未定义的,因为index可能不是data数组范围内的索引。

该函数的主要问题是,如果data是一个词数组,则无法正确迭代它。在这里,每次读取数组时都会递增indexindex每个显示器只应递增一次。

我也不确定我将如何实施延迟。鉴于我已经 以这种方式使用了 setInterval(((我只能使用 setInterval 用于 这样做的目的(我不确定如何让它设置字符串 显示两次,同时还包括延迟。

如果函数必须在无限循环中显示所有单词(这就是index = index % data.length的目的,对吧?(,clearInterval 和另一个 setInterval 可以在传递给当前 setInterval 的匿名函数中调用,从而允许计算你想要的 textSpeed。

每次调用索引变量时index++都会增加索引变量,并且您在循环体中调用它两次。if (!(data[index++].indexOf(punctuation[j]) === -1)) {索引是i,data[index++] = string.replace(punctuation[j], '');是i+1。

代码很混乱,但我希望这就是你想要的......有点:

var str = "some text with no punctuation. And some;: text. with, punctuation? symbols!!? to print"
var print = function(str) {
    var re = /([^ .,:;!?]+)|([.,:;!?]+)/gi
    var reSkip = /[.,:;!?]+/gi           //remove + to add delay for each punctuation symbol instead of the group of symbols
    var substrs = str.match(re)
    var delay = 117
    var timeToPrint = new Date().getTime() + delay
    var i = 0
    console.log(substrs.length)
    var printWord = function() {
        console.log(substrs[i])
        if ( new Date().getTime() < timeToPrint ) {
            console.log(new Date().getTime() +'<'+timeToPrint)
            requestAnimationFrame(printWord)
            return
        }
        if ( reSkip.test(substrs[i]) ) {
            i++
            timeToPrint += delay*5              //to make delay more obvious
            console.log('skip punctuation')
            requestAnimationFrame(printWord)
            return
        }
        document.write(substrs[i++] + ' ')     //replace this with your code to print where you want
        timeToPrint += delay
        console.log('written')
        if (i < substrs.length)
            requestAnimationFrame(printWord)
    }
    printWord()
}
print(str)

只需将其粘贴到chrome控制台即可进行测试。