Reputation: 1858
I need to convert a text entered in a textarea, to a form like:
word1|word2|word3|word4|word5
How can i do this?
Upvotes: 2
Views: 202
Reputation: 6352
Try this...
var textAreaWords=textAreaNode.value.replace(/[^\w ]+/g,'').replace(/\s+/g,'|').split('|');
This will only keep the A-Za-z0-9_ characters as part of the first replace. The second replace turns all spaces/newlines/tabs into pipe characters. It will also convert multiple consecutive spaces into 1 pipe.
Upvotes: 1
Reputation: 3471
This should do the trick:
input = textarea.value.
replace(/\b/g, '|'). // Replace word boundaries with '|'
replace(/\s|[^a-zA-Z0-9\|]/g, ''). // Remove all non-alphanumeric chars
replace(/\|{2,}/g, '|'). // Replace repetitions of '|' (like '||') with '|'
replace(/^\||\|$/g, ''); // Remove extra '|' chars
array = input.split('|');
Upvotes: 2
Reputation: 5593
This should get rid of the tabs, spaces, etc (any unwanted whitespace), and replace them with a '|' character. And, the second replace will get rid of the non-alphanumeric and '|' characters. Then, you can split the text on the '|' to give you an array of the words.
var textIn= document.getElementById("myTextArea");
textIn.value = (textIn.value).replace(/\s+/g,'|').replace(/[^\w|]/g, '');
var textArr = textIn.value.split('|');
Also, if you don't want to actually replace the text in the textarea
, you can store it to a var
instead on the 2nd line of code.
Upvotes: 1
Reputation: 48059
Assuming the user enters the text into the textarea like this:
word1|word2|word3|word4|word5
and you store that in variable string userText
, then use:
var textArray = userText.split('|');
Upvotes: 3