简体   繁体   English

从 textarea 中删除换行符和空格

[英]Remove line-breaks and spaces from textarea

<textarea rows='5' cols='50' id='content'></textarea>
<input type='button' value='Extract Text' onclick='someFunction()'/>

Assume these two html elements.假设这两个 html 元素。 My problem is the following:我的问题如下:

Let's say that the user enters into the textarea field something like this, exactly as typed.假设用户在 textarea 字段中输入的内容与键入的完全相同。 With all line breaks and spaces.带有所有换行符和空格。 (And yes, this is Python :P) (是的,这是 Python :P)

if (...):  
   print "Hello everyone!"
else:
   print "Dudes help me out!"

My objective is to make a JavaScript function which removes all the spaces and line breaks (typed with 'Enter') and returns a space-less string.我的目标是制作一个 JavaScript 函数,它删除所有空格和换行符(用“Enter”输入)并返回一个无空格的字符串。 Something like "ThisIsASampleText".类似于“ThisIsASampleText”。 I've used document.getElementById('content').value and myString.replace(/\\s+/g, '') to remove spaces.我使用document.getElementById('content').valuemyString.replace(/\\s+/g, '')来删除空格。 Can't make it work.不能让它工作。 Any ideas?有任何想法吗?

Best Regards此致

Here's a live demo of how to use the regex you described to remove all whitespace in the text from your textarea:这是如何使用您描述的正则表达式从文本区域中删除文本中的所有空格的现场演示:

 function someFunction() { var output = document.getElementById("content").value; output = output.replace(/\\s/g, "") document.getElementById("output").innerHTML = output; }
 <textarea rows='5' cols='50' id='content'></textarea> <input type='button' value='Extract Text' onclick='someFunction()'/> <div id="output"></div>

JSFiddle Version: https://jsfiddle.net/afy5v66x/1/ JSFiddle 版本: https ://jsfiddle.net/afy5v66x/1/

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

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