最初显示str.replaceAll的Java脚本问题 [英] Java Script problem with initial display of str.replaceAll

查看:69
本文介绍了最初显示str.replaceAll的Java脚本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我提供了用于移动窗口自定进度阅读实验的代码,该代码的初始显示是用破折号(用单词分隔)和区域显示(用分隔的多个单词)覆盖的句子字符串; (在csv中)会在您点击句子时进行迭代.参与者在区域中花费的时间量记录在输出csv中.下面是代码和所需的显示:

I was given this code for a moving window self-paced reading experiment where the initial display is a sentence string covered by dashes (separated by word) and the display of regions (multiple words separated by "," in the csv) is iterated as you click through the sentence. The amount of time the participant spends in a region is recorded in the output csv. Below is the code and desired display:

var _pj;
function replaceWithdash(Sentence, currentWordNumber) {
const sentenceList = Sentence.split(",")
const sigil = sentenceList.map(s => s.replaceAll(/[^\s]/g, "-"))
if (currentWordNumber !== undefined) {
    sigil.splice(currentWordNumber, 1, sentenceList[currentWordNumber])
}
return sigil.join("")
}
function _pj_snippets(container) {
    function in_es6(left, right) {
        if (((right instanceof Array) || ((typeof right) === "string"))) {
            return (right.indexOf(left) > (- 1));
        } else {
            if (((right instanceof Map) || (right instanceof Set) || (right instanceof WeakMap) || (right instanceof WeakSet))) {
                return right.has(left);
            } else {
                return (left in right);
            }
        }
    }
    container["in_es6"] = in_es6;
    return container;
}
_pj = {};
_pj_snippets(_pj);
Trials_Display.text = replaceWithdash(Sentence, wordNumber);
keypresses = psychoJS.eventManager.getKeys();
sentenceList = Sentence.split(",");
if ((keypresses.length > 0)) {
    if (_pj.in_es6("space", keypresses)) {
        thisResponseTime = t;
        wordNumber = (wordNumber + 1);
        if ((wordNumber < sentenceList.length)) {
            if ((wordNumber === 0)) {
                timeOfLastResponse = 0;
            }
            thisExp.addData(("IRI_" + wordNumber.toString()), (thisResponseTime - timeOfLastResponse));
            timeOfLastResponse = thisResponseTime;
            Trials_Display.text = replaceWithdash(Sentence, wordNumber);
        } else {
            continueRoutine = false;
        }
    } else {
        if (_pj.in_es6("escape", keypresses)) {
            core.quit();
        }
    }
}

所需显示:

str = "The dog, ate, the food"

console.log(replaceWithdash(str))
//"--- --- --- --- ----"

console.log(replaceWithdash(str, 0))
//"The dog --- --- ----"

console.log(replaceWithdash(str, 1))
//"--- --- ate --- ----"

console.log(replaceWithdash(str, 2))
// "--- --- --- the food"

尽管此代码确实成功显示了破折号之间的空格并逐个区域显示,但我的问题是破折号句子的初始显示.看起来它仅显示前两个虚线区域,然后单击以显示第一个区域中的单词,它将进行调整以显示第三个区域的破折号.同样,由于某种原因,区域之间似乎还有多余的空间.像这样:

Although this code does successfully show spaces between dashes and displays region by region, my problem is with the initial display of the dashed sentence. It looks like it's only showing the first two dashed regions and then once you click to reveal the words in the first region, it'll adjust to show the dashes for the third region. Also, there seems to be an extra space between regions for some reason. Something like this:

 str = "The dog, ate, the food"

//"--- ---  ---"

//"The dog  --- ---  ----"

//"--- ---  ate  --- ----"

// "--- ---  ---  the food"

我认为没有索引(console.log(replaceWithdash(str)))解释初始字符串的方式有问题,但我不确定如何解决此问题或多余的空格.除了自己学过的知识之外,我对Java脚本还比较陌生,因此不胜感激!

I assume there's something wrong with how it's interpreting the initial string without an index (console.log(replaceWithdash(str)), but I'm not sure how to fix this, or the extra spaces. I'm relatively new to Java script besides what I've taught myself, so any help would be appreciated!!

推荐答案

哇,TLDR注释了.这是我的做法:

Wow, TLDR the comments. Here's how I'd do it:

function replaceWithDash(input,partNumber) {
    //First, lets check if partNumber was omitted, since we can use this case
    //to replace a single part as well
    if(typeof partNumber!="number")
        //Just remove the commas and replace every letter with the dash
        return input.replace(/,/g,"").replace(/[^\s]/g,"-");

    //If partNumber was specified (the function didn't end on the previous return), lets split the sentence
    parts=input.split(",");
    //remove the partNumber-th element and replace it with its dashed version
    parts.splice(partNumber,1,replaceWithdash(parts[partNumber]));
    //then join everything again and fix repeating spaces (it's quicker to do this once here than trimming each element)
    return parts.join(" ").replace(/[\s]+/g," ");
}

请注意,我固定了函数名称.

Note that I fixed the function name.

这篇关于最初显示str.replaceAll的Java脚本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆