2017-02-14 58 views
0

我想用另一個字符串替換字符串中的字符(original)。
我在運行調試器時出錯。
我不明白語法有什麼問題。未捕獲ReferenceError:賦值中無效的左側。 Javascript

<!DOCTYPE> 
 
<html> 
 

 
<head> 
 
    <title>HI there</title> 
 
    <meta lang="english"> 
 
</head> 
 

 
<body> 
 
    <div> 
 
     Enter the original string<input id="original" value="" type="text"> <br> 
 
     Enter the replacing string<input id="replacing" value="" type="text"><br> 
 
     Enter the location to be replaced<input id="tobereplaced" value="" type="text"><br> 
 
    </div> 
 
    <br> 
 
    <button type="submit" onclick="replace()">Submit</button> 
 
    <br> Here you go the replaced string is: 
 

 
    <script> 
 
     function replace() { 
 
      var original = document.getElementById("original").value; 
 
      var replacing = document.getElementById("replacing").value; 
 
      var tobereplaced = document.getElementById("tobereplaced").value; 
 
      var replaced = ""; 
 
      var originalLength = original.length; 
 
      var tobereplacedLength = tobereplaced.length; 
 
      var k = 0; 
 
      for (var i = 0; i < originalLength; i++) { 
 
       replaced.charAt(k) = original.charAt(i) 
 
       if (original.charAt(i) == replacing.charAt(0)) { 
 
        replaced = replaced + tobereplaced; 
 
        k = k + tobereplacedLength; 
 
        i++; 
 
       } 
 
       k++; 
 
      } 
 
      document.getElementById("replaced").innerHTML = replaced; 
 

 
     } 
 
    </script> 
 
    <h1 id="replaced"></h1> 
 
</body> 
 

 
</html>

+0

'replaced'是空字符串。這就是爲什麼你不能通過charAt()來訪問它。 – sandrooco

+0

是的。謝謝@Sandrooco –

回答

1

你正在試圖在沒有線改變一個空字符串的字符:28 [replaced.charAt(K)= original.charAt(I)],這是問題。 也有一些不必要的代碼增量。請在下面更正

我已更新下面的代碼//註釋代碼並添加了正確的代碼。其工作

 // var k = 0; //Commented 
     // debugger; //Commented 
     for (var i = 0; i < originalLength; i++) { 

      if (original.charAt(i) == replacing.charAt(0)) { 
       replaced = replaced + tobereplaced; 
       // k = k + tobereplacedLength; //Commented 
       // i++; //Commented 
      } else{ 
      replaced = replaced + original.charAt(i); 
      } 
      // k++; //Commented 
     } 
+0

非常感謝。 –

+0

我改進了代碼以重複字符並在上面更新。現在它的工作很完美。如果您需要任何幫助,請告訴我。 –

+0

是的。再次感謝。 –

0

這是一個更簡單的方法來解決這個問題。它利用.split()& .join()函數而不是使用for循環。

function replace() { 
 
    // set original string 
 
    var original = document.getElementById("original").value, // << use commas so you don't have to keep typing var 
 
    // set replacing string 
 
    replacing = document.getElementById("replacing").value, 
 
    // initialize newval 
 
    newValue, 
 
    // set replace position - this could also be called index 
 
    replacePosition = document.getElementById("tobereplaced").value; // << end variable declarations with semicolon 
 

 
    // split original string into array of characters 
 
    var splitOriginal = original.split(""); 
 
    // use replacePosition as index value of character to replace 
 
    // & replace that character with replacing value 
 
    splitOriginal[replacePosition] = replacing; 
 
    // join array to form new string value 
 
    newValue = splitOriginal.join(""); 
 

 
    document.getElementById("replaced").innerHTML = newValue; 
 

 
}
<html> 
 

 
<head> 
 
    <title>HI there</title> 
 
    <meta lang="english"> 
 
</head> 
 

 
<body> 
 
    <div> 
 
    Enter the original string 
 
    <input id="original" value="" type="text"> 
 
    <br>Enter the replacing string 
 
    <input id="replacing" value="" type="text"> 
 
    <br>Enter the location to be replaced 
 
    <input id="tobereplaced" value="" type="text"> 
 
    <br> 
 
    </div> 
 
    <br> 
 
    <button type="submit" onclick="replace()">Submit</button> 
 
    <br>Here you go the replaced string is: 
 

 

 
    <h1 id="replaced"></h1> 
 
</body> 
 

 
</html>

+0

是的,這工作得很好。但是我試圖在不使用內置函數的情況下工作。無論如何感謝兄弟。 –

+0

@bharathkumarreddy哦,我的壞。我意識到你在帖子中沒有jQuery標籤。 jQuery是一個功能強大的JavaScript庫。如果你還沒有學到它,我會推薦它! – Inkdot

+0

是的,我最近開始學習網絡開發。什麼技術是強制性的?謝謝。 –

相關問題