简体   繁体   English

Javascript中的正则表达式来分隔单词

[英]Regular Expression in Javascript to delimit words

I need to convert a text entered in a textarea, to a form like: 我需要将在textarea中输入的文本转换为如下形式:

word1|word2|word3|word4|word5 字词1 | WORD2 | WORD3 | word4 |的word5

How can i do this? 我怎样才能做到这一点?

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: 并将其存储在变量字符串userText ,然后使用:

var textArray = userText.split('|');

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('|');

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. 另外,如果您不想实际替换textarea中的textarea ,则可以将其存储到var而不是代码的第二行。

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. 这将仅保留A-Za-z0-9_字符作为第一次替换的一部分。 The second replace turns all spaces/newlines/tabs into pipe characters. 第二次替换将所有空格/换行符/制表符转换为竖线字符。 It will also convert multiple consecutive spaces into 1 pipe. 它还会将多个连续的空格转换为1个管道。

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

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