[英]How to split words using javascript
This might be a simple question but, how do i split words... for example 这可能是一个简单的问题,但是,我如何分割单词...例如
a = "even, test"
I have used .split
to 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 谢谢
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. 但在你的情况下,你可能不需要那么远。
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>
I found a list of word separators in Sublime Text default settings. 我在Sublime Text默认设置中找到了一个单词分隔符列表。 Here's how to split with it, with some Unicode support (the defined separators are not Unicode though):
以下是如何使用一些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(在
-
)将空格数作为分隔符
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
a.split(',')
or 要么
var re = /\s*,\s*/
var newA = a.split(re);
Just use this code: 只需使用此代码:
var a = "even, test";
var words = a.split(", ");
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初学者,所以这可能不是最好的方法,但它仍然有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.