2015-01-10 40 views
0

我想做一個文本解析器的種類,我做了大量的研究。JS文本解析器不工作

我想知道如果我正確使用JavaScript。我知道你不應該在js中創建文本解析器,但它是我知道的唯一腳本語言。

這裏是我到目前爲止的代碼:

<script> 
    function makeCode() { 
    for (i = 0; i < userCode.length; i++) { 
    if (userCode.substring("print:"(0, 6)) { 
     document.write(usercode.substring("print:"(6,0)); 
    } 
</script> 

<style> 
    p { 
    text-align: center; 
    color: #cc0000; 
    } 
    h1 { 
    text-align: center; 
    font-family: Impact; 
    color: #cc0000; 
    } 
    body { 
    text-align: center; 
    } 
    form { 
    text-align: center; 
    } 
</style> 
<h1>NCPL</h1> 
<p style="font-size: 10px">In Browser NinjaCorp Programming Language Editor</p> 
</head> 
<body> 
<form name="userCode"> 
    <textarea id="userCode" name="userCode" cols="80" rows="20" placeholder="Type your code  here"></textarea></br> 
    <a href="javascript:makeCode()"><button type="button">Run Code!</button></a> 
</form> 
+2

什麼問題了嗎? – theonlygusti

+0

不使用document.write()它可以是一種評估形式。 – jmingov

+0

''userCode''在你的函數中是未定義的。你必須首先從DOM中獲取它,類似於'var userCode = document.getElementById('userCode').value''。此外,你使用子串是沒有意義的。那裏的固定字符串「打印」是什麼?當用戶點擊按鈕時應該發生什麼? – Antares42

回答

0

這是你在找什麼?

function makeBode() { 
 
    var userCode = document.getElementById('userCode').value; 
 
    var myDiv = document.getElementById('myDiv'); 
 
    
 
    // alert(userCode) 
 
    for (i = 0; i < userCode.length; i++) { 
 
     myDiv.innerHTML = myDiv.innerHTML + userCode.substring((i,i+6)) +'<br>'; 
 
    } 
 

 
}
p { 
 
    text-align: center; 
 
    color: #cc0000; 
 
    } 
 
    h1 { 
 
    text-align: center; 
 
    font-family: Impact; 
 
    color: #cc0000; 
 
    } 
 
    body { 
 
    text-align: center; 
 
    } 
 
    form { 
 
    text-align: center; 
 
    }
<div id="myDiv"></div> 
 

 
<h1>NCPL</h1> 
 
<p style="font-size: 10px">In Browser NinjaCorp Programming Language Editor</p> 
 
</head> 
 
<body> 
 
    
 
<form name="userCode"> 
 
    <textarea id="userCode" name="userCode" cols="80" rows="20" placeholder="Type your code  here">Typing here so there is some text to work with </textarea></br> 
 
    <a href="javascript:makeBode();"><button type="button">Run Code!</button></a> 
 
    
 
</form>

+0

不,我一直在尋找一些東西,使它不會說打印和說後面的文字。不管怎麼說,還是要謝謝你 – tacomanmagic