简体   繁体   English

使用javascript将文本拆分成单个单词,每个单词一行

[英]Split an text into single words, each on a line, with javascript

I wrote this code, with javascript incorporated in html, in order to split the entered text in the input area into single words, each on a line: 我编写了这段代码,并将javascript合并到html中,以便将输入区域中的输入文本分成单个单词,每个单词一行:

 function convertToWords() { var MyVar; MyVar = document.getElementById("demo"); console.log(MyVar.value.replace(/ /g, "\\n")); var x = document.getElemenyById("myDiv"); x.innerHTML = MyVar.value.replace(/ /g, "\\n"); } 
 <input type="text" value="" id="demo" style="width:100%; height:100px"> </input> <input type="button" value="Convert to single words" onclick="convertToWords();"> </input> <br> <div id="myDiv"></div> 

My question, how to let the result be in html for example in a div instead just only as console log? 我的问题是,如何让结果以html为例,例如在div中,而不只是仅作为控制台日志?

I tried the highlighted code in the image, but doesnt work :( 我尝试了图像中突出显示的代码,但不起作用:(

Thanks for any help 谢谢你的帮助

You have a typo in your function name: getElementById instead of getElemenyById . 函数名称中有一个错字: getElementById而不是getElemenyById Also as everybody else suggest, you can use a <br> to put it on a new line. 另外,正如其他人所建议的那样,您可以使用<br>将其放在新的一行。

However look at this solution, it uses more robust coding. 但是,请看此解决方案,它使用更强大的编码。

  • \\s as the regex, it splits on all whitespaces. \\s作为正则表达式,它在所有空格上分割。 Use this site a nice reference http://www.regular-expressions.info/tutorial.html and https://regex101.com/ to test your regex. 使用此站点可以很好地参考http://www.regular-expressions.info/tutorial.htmlhttps://regex101.com/来测试您的正则表达式。
  • split , it turns the words into an array. split ,它把单词变成一个数组。 So you can do all kinds tricks with it. 因此,您可以使用它来做各种花样。
  • then in the loop, we build block level ( div ) elements (which are by default placed on a new line) and append them to the result div. 然后在循环中,我们构建块级( div )元素(默认情况下放置在新行上)并将它们附加到结果div中。

 //lets improve this: use eventlisteners instead of inline events document.querySelector("input[type='button']").addEventListener("click", convertToWords, false); //using document.querySelector to select the input button based on its node name and type. // then add a click event to it that refers to convertToWords. function convertToWords() { var MyVar; MyVar = document.getElementById("demo"); var resultDiv = document.getElementById("myDiv"); //try to use descriptive names. // the use of <br> is not really nice, it works, but this is cleaner var wordArray = MyVar.value.split(/\\s/); //using \\s to split on all white spaces wordArray.forEach(function(element){ //loop over every entry in the array using foreach var node = document.createElement("div"); //create a block node element; node.textContent = element; //fill div with the text entry. resultDiv.appendChild(node); //set node to the result div }); } 
 <input type="text" value="" id="demo" style="width:100%; height:100px" /> <input type="button" value="Convert to single words" /> <br> <div id="myDiv"></div> 

Functions used that are interesting to get familiar with: 熟悉的有趣功能包括:

you can use <br> instead of \\n 您可以使用<br>代替\\n

 function convertToWords() { var MyVar; MyVar = document.getElementById("demo"); var x = document.getElementById("myDiv"); x.innerHTML = MyVar.value.replace(/ /g, "<br/>"); } 
 <input type="text" value="" id="demo" style="width:100%; height:100px"> </input> <input type="button" value="Convert to single words" onclick="convertToWords();"> </input> <br> <div id="myDiv"></div> 

Use <br> instead of \\n to break line in html. 使用<br>而不是\\n在html中换行。

 function convertToWords() { var MyVar = document.getElementById("demo"); //console.log(MyVar.value.replace(/ /g, "\\n")); var x = document.getElementById("myDiv"); x.innerHTML = MyVar.value.replace(/ /g, "<br>"); } 
 <input type="text" value="" id="demo" style="width:100%; height:100px"> <input type="button" value="Convert to single words" onclick="convertToWords();"> <br > <div id="myDiv"></div> 

Since the browser doesn't care about \\n , you can replace the match (a space) with a break. 由于浏览器不在乎\\n ,因此您可以将匹配项(空格)替换为分号。

 document.getElementById("input").addEventListener('keyup', function(e) { document.getElementById("output").innerHTML = e.target.value.replace(/\\s/g, "<br/>"); }); 
 <input type="text" id="input"> <div id="output"></div> 

这应该工作

 x.innerHTML = MyVar.value.replace(/ /g, "<br />");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM