Javascript 如何使用javascript拆分单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7660756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:05:56  来源:igfitidea点击:

How to split words using javascript

javascriptsplit

提问by Harry

This might be a simple question but, how do i split words... for example

这可能是一个简单的问题,但是,我如何拆分单词...例如

a = "even, test"

I have used .splitto seperate the text with space.

我曾经用.split空格分隔文本。

so the result came is like

所以结果就像

a = "even,"
b = "test"

But, how do I remove the 'comma' here?

但是,如何删除此处的“逗号”?

But in some conditions it might get "even test" and in some conditions i might get "even, test". All are dynamic, so how do i check it for both?

但在某些情况下,它可能会得到“均匀测试”,而在某些情况下,我可能会得到“均匀测试”。所有都是动态的,那么我如何检查两者?

Thanks

谢谢

回答by Spudley

Firstly, the split()function is not jQuery - it is pure Javascript.

首先,该split()函数不是 jQuery - 它是纯 Javascript。

Did you try doing split with a comma and a space? That would have worked just fine in your case:

您是否尝试使用逗号和空格进行拆分?在您的情况下,这会很好用:

var result = input.split(', ');

For more complex splits, you can use regular expression pattern matching to allow multiple commas or spaces between the two fields:

对于更复杂的拆分,您可以使用正则表达式模式匹配来允许两个字段之间有多个逗号或空格:

var result = input.split(/[, ]+/);

but you probably don't need to go that far in your case.

但在您的情况下,您可能不需要走那么远。

回答by ESL

I think is better to use something like this:

我认为最好使用这样的东西:

text.match(/[a-z'\-]+/gi);

Example:

例子:

var e=function()
 {
  var r=document.getElementById('r');
  var x=document.getElementById('t').value.match(/[a-z'\-]+/gi);
  for(var i=0;i<x.length;i++)
   {
    var li=document.createElement('li');
    li.innerText=x[i];
    r.appendChild(li);  
   }
 }
<div style="float:right;width:18%">
 <ol id="r" style="display:block;width:auto;border:1px inner;overflow:scroll;height:8em;max-height:10em;"></ol>
 <button onclick="e()">Extract words!</button>
</div>
<textarea id="t" style="width:70%;height:12em">even, test; spider-man

But saying o'er what I have said before:
My child is yet a stranger in the world;
She hath not seen the change of fourteen years,
Let two more summers wither in their pride,
Ere we may think her ripe to be a bride.

—Shakespeare, William. The Tragedy of Romeo and Juliet</textarea>

回答by Rivenfall

I found a list of word separators in Sublime Text default settings. Here's how to split with it, with some Unicode support (the defined separators are not Unicode though):

我在 Sublime Text 默认设置中找到了一个单词分隔符列表。下面是如何用它分割,有一些 Unicode 支持(虽然定义的分隔符不是 Unicode):

{ // word_separators: ./\()"'-,;<>~!@#$%^&*|+=[]{}`~?: (32)
    function splitByWords(str = '', limit = undefined) {
        return str.split(/[-./\()"',;<>~!@#$%^&*|+=[\]{}`~?:]/u, limit)
    }

    function reverseString(str) {
        let newString = ''
        for (let i = str.length - 1; i >= 0; i--)
            newString += str[i]
        return newString
    }

    const str = '123.x/x\x(x)x"x\'x-x:x,789;x<x>x~x!x@x#x$x%x^x&x*x|x+x=x[x]x{x}x`x~x?456'
    console.log(splitByWords(str)) // (33)?["123", "x", "x", "x", "x", "x", "x", "x", "x", "x", "789", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "456"]
    console.log(splitByWords(str, 1)) // ["123"]
    console.log(splitByWords(reverseString(str), 1)) // ["654"]
}

For some reason the -has to be at the beginning, and the :at the end. Edit: you might want to add \s (after the -) to count whitespace as separator

出于某种原因-,必须在开头和:结尾。编辑:您可能想要添加 \s (在 之后-)以将空格计为分隔符

回答by megakorre

Hej Harry

海吉·哈里

if the comma is the separator you can call split with the comma

如果逗号是分隔符,您可以使用逗号调用 split

Ex:

前任:

var newColls = myString.split(",");

and not split with space.

并且不与空间分开。

GL

GL

回答by v100ev

a.split(',')

or

或者

var re = /\s*,\s*/
var newA = a.split(re);

回答by Dmytro Shevchenko

Just use this code:

只需使用此代码:

var a = "even, test";
var words = a.split(", ");

回答by r0skar

I think you could do it like this:

我认为你可以这样做:

var a= 'even,'
var newA = a.slice(0, -1)

This will remove the last char from a given string.

这将从给定的字符串中删除最后一个字符。

And to check if the string contains a comma, I would do the following:

并检查字符串是否包含逗号,我将执行以下操作:

if (a.indexOf(",") >= 0){
    //contains a comma
} else {
    //no comma
}

I am a javascript beginner, so this probably is not the best way to do it, but nevertheless it works.

我是一个 javascript 初学者,所以这可能不是最好的方法,但它仍然有效。