简体   繁体   中英

How can I get words from a string, textarea javascript

I've been working on JavaScript and HTML, I have a text area where the user sets a CSV like this:

17845    hello     bye    789

Now I have 17845,hello,bye,789 and I need to extract the values between the commas. I've tried with index Of, but what if the user sets 2 lines instead of 1, how can I get these words? I have thought of separate them getting the "\\n" .

Javascript函数split()将解决问题

var str = '17845,hello,bye,789';
var words = str.split(',');

console.log(words);

使用javascript split()函数

Split function gives u the array.

 var sentence ="hello, 123, tedsfd, demo"; var strArr = sentence.split(','); $.each(strArr,function(key,value){ console.log(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

如果我对它的理解正确,那么如果用户在多行而不是一行中设置逗号分隔值,则会遇到问题。为此,请使用trim()函数删除所有制表符和换行符,然后使用split()函数。

Use String.split .
It converts strings into arrays, given you provide the delimiter, to separate the string.

var userInput = '17845, hello, bye, 789';

data = userInput.split(',');

console.log(data);
//data[0] = '17845'
//data[1] = 'hello'
//data[2] = 'bye'
//...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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