javascript - Coderbyte 挑战 : Questions Marks - RegExp pattern '/d(\?\?\?)d/gi' incorect

标签 javascript regex

我正在解决Coderbyte Challenge - Questions Marks 当我在浏览器中运行代码时,一切正常,但是,一旦我在 coderbyte 网站上运行它,就会抛出错误。

挑战是:

Have the function QuestionsMarks(str) take the str string parameter, which will contain single digit numbers, letters, and question marks, and check if there are exactly 3 question marks between every pair of two numbers that add up to 10. If so, then your program should return the string true, otherwise it should return the string false. If there aren't any two numbers that add up to 10 in the string, then your program should return false as well.

For example: if str is "arrb6???4xxbl5???eee5" then your program should return true because there are exactly 3 question marks between 6 and 4, and 3 question marks between 5 and 5 at the end of the string.

Use the Parameter Testing feature in the box below to test your code with different arguments.

测试用例是:

"arrb6???4xxbl5???eee5" true

"aa6?9" false

"acc?7??sss?3rr1??????5" true

我的解决方案是使用 RegExp 来解决这个挑战。当我在浏览器中运行下面的代码时,它运行良好,但是,Coderbyte 控制台每次都会抛出错误:

/tmp/009904362/main.js:11 clean = clean.match(/d(???)d/gi); ^SyntaxError: Invalid regular expression: /d(???)d/

这是我的代码-

function QuestionsMarks(str) { 

//create a "clean" array containing only the numbers and question marks from str
  var result;
  let clean = str.match(/[0-9?]/g);
// join() the array back in to the string
  clean = clean.join("");     

// use match() to return an array of pairs that match the pattern d???d  
 clean = clean.match(/d(\?\?\?)d/gi);
 
//create a function sumCheck() that converts first and last char of every array string to Number and checks if the sum of digits is 10
//using forEach() run the sumcheck() on all strings in the array
 clean.forEach(sumCheck);

 function sumCheck(string){
        if((Number(string.charAt(0)) + Number(string.charAt(string.length - 1)))  == 10){
                result = true;
        }else{
            result = false;
        }
 }
    return result;
  }
QuestionsMarks("acc?7??sss?3rr1??????5");

最佳答案

问题似乎来自 Coderbyte,它无法正确解析正则表达式模式(文字或使用 RegExp 构造函数)中的转义字符。因此,最简单的解决方案是替换转义序列: \d => [0-9]\? => [ ?] (正如 @Saud 在评论中建议的那样)。


关于你的方法:

... check if there are exactly 3 question marks between every pair of two numbers that add up to 10 ...

您的更正模式 /[0-9][?]{3}[0-9]/g 是什么?
它查找由三个问号分隔的数字(并且然后检查两位数之和是否为 10)。即使此模式能够找到字符串中由三个问号分隔的所有数字对(情况并非如此(*)),它也不会检查是否有数字加起来为 10,并且它们之间没有正好 3 个问号!

因此,目标是查找字符串是否包含一对加起来等于 10 的数字,没有 3 个问号。如果该对存在,则函数返回false


(*): 为什么/[0-9][?]{3}[0-9]/g无法找到所有由3个问号分隔的数字对?

示例:1???2???3???4
因为你不能将同一个字符匹配两次。该模式将找到:1???23???4,但不会找到 2???3,因为 2 已被第一场比赛消耗。


一种可能的方法:

function QuestionsMarks(str) {
    var state = { d1: 0, d2: 0, marks: 0,
        init: function() { this.d1 = this.d2; this.marks = 0; },
        check: function() { return this.d1 + this.d2 > 9 && this.marks != 3; }
    };  
    var re = /[0-9?]/g;
    var m;

    while ( (m = re.exec(str)) !== null ) {
        if ( m[0] == '?' ) {
            state.marks++;
        } else {
            state.d2 = parseInt(m[0]);
            if ( state.check() ) return false;
            state.init();  
        }
    }
    return true;
}

关于javascript - Coderbyte 挑战 : Questions Marks - RegExp pattern '/d(\?\?\?)d/gi' incorect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51696966/

相关文章:

javascript - 回调已经被调用。但在此之前我从未调用过回调。 async.parallel 中的错误?

regex - 如何在 Scala 中通过正则表达式生成字符串

php - 在 RegEx 中接受国际名称字符

regex - 如何在 Perl 中将正则表达式模式定义为常量?

php - 用正则表达式包裹 <a> 标签中的链接

javascript - 用于从带有子 div 的链接中选择文本的 xpath 表达式

JavaScript:异步事件回调的线程安全? (我需要 'volatile' 还是什么?)

Javascript继承问题

javascript - document.querySelectorAll 和 document.getElementsByClassName 事件处理

javascript - 点击加载 html 时的 JQuery