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>
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.