Javascript: 检查字符串是否包含字符,然后删除字符串的一部分?
Javascript: Checking if string contains a character, then removing part of a string?
本质上,此功能应该做的是从输入文本中获取单词列表,并以客户选择的时间间隔 (WPM) 从下拉菜单中将其设置在显示器上。
如果传入函数的单词中包含问号、句点、冒号、分号、感叹号或逗号,则将其删除,并将所选间隔加倍。例如,如果单词之间的延迟为 117 毫秒,则为 234 毫秒。
我在确定传递的单词是否包含标点符号并将其删除的部分中遇到了最大的麻烦。
我遇到一个错误:
未捕获的类型错误:无法读取未定义的 属性 'indexOf'。
我不确定为什么会这样,因为 list[index++]
是 String
而 indexOf
是 Javascript 中的字符串方法,而不是 [=31] =].
我也不确定如何实施延迟。鉴于我已经以这种方式使用了 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);
}
}
index++
每次调用时都会增加索引变量,并且您在循环体中调用了两次。
在 if (!(data[index++].indexOf(punctuation[j]) === -1)) {
中索引是 i,在 data[index++] = string.replace(punctuation[j], '');
中是 i+1。
I'm getting an error: Uncaught Type Error: Cannot read property
'indexOf' of undefined.
I'm not sure why this is happening since list[index++] is a String and
indexOf is a method of Strings in Javascript and not a property.
首先,方法也是对象的属性。
其次,JS 引擎告诉您,您正在对 undefined
调用 indexOf
,因此它不是字符串。并且 data[index++]
未定义,因为 index
可能不是 data
数组范围内的索引。
该函数的主要问题是,如果 data
是一个单词数组,您无法正确迭代它。在这里,每次读取数组时都会递增 index
,每次显示时 index
应该只递增一次。
I'm also not sure how I would implement the delay. Given that I've
used setInterval() in this way (and I can only use setInterval for the
purposes of this) I'm not sure how I would get it to set the String in
the display twice while also including the delay.
如果函数必须在无限循环中显示所有单词(这就是 index = index % data.length
的目的,对吗?),可以在传递给当前 setInterval 的匿名函数中调用 clearInterval 和另一个 setInterval,允许计算你想要的 textSpeed。
代码很乱,但我希望这是你想要的...有点:
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 控制台进行测试。
本质上,此功能应该做的是从输入文本中获取单词列表,并以客户选择的时间间隔 (WPM) 从下拉菜单中将其设置在显示器上。
如果传入函数的单词中包含问号、句点、冒号、分号、感叹号或逗号,则将其删除,并将所选间隔加倍。例如,如果单词之间的延迟为 117 毫秒,则为 234 毫秒。
我在确定传递的单词是否包含标点符号并将其删除的部分中遇到了最大的麻烦。
我遇到一个错误: 未捕获的类型错误:无法读取未定义的 属性 'indexOf'。
我不确定为什么会这样,因为 list[index++]
是 String
而 indexOf
是 Javascript 中的字符串方法,而不是 [=31] =].
我也不确定如何实施延迟。鉴于我已经以这种方式使用了 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);
}
}
index++
每次调用时都会增加索引变量,并且您在循环体中调用了两次。
在 if (!(data[index++].indexOf(punctuation[j]) === -1)) {
中索引是 i,在 data[index++] = string.replace(punctuation[j], '');
中是 i+1。
I'm getting an error: Uncaught Type Error: Cannot read property 'indexOf' of undefined.
I'm not sure why this is happening since list[index++] is a String and indexOf is a method of Strings in Javascript and not a property.
首先,方法也是对象的属性。
其次,JS 引擎告诉您,您正在对 undefined
调用 indexOf
,因此它不是字符串。并且 data[index++]
未定义,因为 index
可能不是 data
数组范围内的索引。
该函数的主要问题是,如果 data
是一个单词数组,您无法正确迭代它。在这里,每次读取数组时都会递增 index
,每次显示时 index
应该只递增一次。
I'm also not sure how I would implement the delay. Given that I've used setInterval() in this way (and I can only use setInterval for the purposes of this) I'm not sure how I would get it to set the String in the display twice while also including the delay.
如果函数必须在无限循环中显示所有单词(这就是 index = index % data.length
的目的,对吗?),可以在传递给当前 setInterval 的匿名函数中调用 clearInterval 和另一个 setInterval,允许计算你想要的 textSpeed。
代码很乱,但我希望这是你想要的...有点:
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 控制台进行测试。