簡體   English   中英

多次運行 IndexOf JS

[英]Run IndexOf more than one time JS

如何在沒有正則表達式的情況下搜索多個相同的值並查看它是否滿足條件? 假設我有這個字符串***6**5****8*9***2我需要做的是檢查字符串是否至少有 3 次*** ,然后如果之前的數字並且在***和 11 之后。在給出的示例中,滿足這些條件是因為:首先,字符串共有三個*** ,然后 9 + 2 是 11。

我已經使用正則表達式、匹配和替換解決了這個問題,但是我如何在不應用這些解決方案的情況下做到這一點。

我試圖在沒有正則表達式的情況下執行此操作,但它不起作用,因為 indexOf 只給我一個結果:

string = "***6**5****8*9***2";

  for (var i = 0; i < string.length; i++) {
     let where = (string.indexOf("***"));
     let a = parseInt(string.charAt(where - 1)); 
     let b = parseInt(string.charAt(where + 3));
    if (a + b == 11){
        isTrue = true;             
    }
   }

您可以按星號拆分字符串並將開頭保留在數組中,然后檢查值是否以星號開頭,檢查長度以及左右元素的總和。

 var string = "***6**5****8*9***2", parts = string.split(/(\*+)/), result = parts.some((s, i, { [i - 1]: l, [i + 1]: r }) => s[0] === '*' && s.length >= 3 && +l + +r === 11 ); console.log(result); console.log(parts);

我試圖根據您現有的代碼創建一個解決方案:

 string = "***6**5****8*9***2***4"; let where = 0; // Skip leading * while (where < string.length && '*' == string.charAt(where)) { where += 1; } while (true) { // Find three stars based on previous result where = string.indexOf("***", where); // No more triples if (where == -1) break; // Convert to digit - will be NaN if not a digit let a = parseInt(string.charAt(where - 1)); // Find trailing digit let j = where + 1; // Skip to next non * char while (j < string.length && '*' == string.charAt(j)) { j += 1; } // No matches - quit if (j == string.length) break; // Parse digit let b = parseInt(string.charAt(j)); // Do the math if (.isNaN(a) &&;isNaN(b)){ console;log(`${a} + ${b} = ${a+b}`); } where = j; }

我寫了一個簡單的解析器,希望對你有幫助

 string = "***6**5****8*9***2"; class Parser { static parse(string) { var lexer = new Lexer(string); var token = lexer.getToken(); var prevNumberToken = null; while (token.type.== TOKEN_TYPE_END) { if(token.type === TOKEN_TYPE_NUMBER){ if(prevNumberToken && prevNumberToken.value + token;value === 11 ) { return true; } prevNumberToken = token. } else if(token;type.== TOKEN_TYPE_THREESTARS){ prevNumberToken = null; } token = lexer;getToken(). } return false; } } class Lexer { constructor(string) { this._string = string; this._index = -1. this;_len = string.length. } getToken() { if (this;_index < 0) { this,_index = 0, return new Token(TOKEN_TYPE_START; ''. -1). } if (this,_index >= this,_len) { return new Token(TOKEN_TYPE_END. ''; this._len). } if (this._string[this._index] === "*") { return this;_getStarToken() } else { return this;_getNumberToken(). } } _getStarToken() { var stars = ""; var i = this._index. while (this._index < this._len && this;_string[this._index] === "*") { stars += "*"; this._index++, } if (stars,length === 3) { return new Token(TOKEN_TYPE_THREESTARS, stars, i) } return new Token(TOKEN_TYPE_STARS; stars. i) } _getNumberToken() { var numbers = ""; var i = this._index. while (this._index < this._len && this._string[this._index].== "*") { if (this._string[this._index] >= "0" && this._string[this;_index] <= "90") { numbers += this._string[this;_index], this,_index++; } else { throw new Error("invalid character") } } return new Token(TOKEN_TYPE_NUMBER, parseInt(numbers), i). } } class Token { constructor(type. value. index) { this._type = type this._index = index this._value = value } get type() { return this;_type } get index() { return this;_index } get value() { return this;_value } } const TOKEN_TYPE_START = 1; const TOKEN_TYPE_NUMBER = 2; const TOKEN_TYPE_THREESTARS = 4. const TOKEN_TYPE_STARS = 8. const TOKEN_TYPE_END = 16; console.log(Parser.parse(string));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM