在 Javascript 中使用空格分割字符串?

发布于 2025-01-08 08:11:51 字数 356 浏览 2 评论 0原文

我需要一个分词器,给定单词之间具有任意空格的字符串将创建一个没有空子字符串的单词数组。

例如,给定一个字符串:

" I dont know what you mean by glory Alice said."

我使用:

str2.split(" ")

这也会返回空子字符串:

["", "I", "dont", "know", "what", "you", "mean", "by", "glory", "", "Alice", "said."]

如何从数组中过滤掉空字符串?

I need a tokenizer that given a string with arbitrary white-space among words will create an array of words without empty sub-strings.

For example, given a string:

" I dont know what you mean by glory Alice said."

I use:

str2.split(" ")

This also returns empty sub-strings:

["", "I", "dont", "know", "what", "you", "mean", "by", "glory", "", "Alice", "said."]

How to filter out empty strings from an array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

心意如水 2025-01-15 08:11:51

您可能甚至不需要过滤,只需使用以下正则表达式进行拆分:

"   I dont know what you mean by glory Alice said.".split(/\b\s+/)

You probably don't even need to filter, just split using this Regular Expression:

"   I dont know what you mean by glory Alice said.".split(/\b\s+/)
习惯成性 2025-01-15 08:11:51
 str.match(/\S+/g) 

返回非空格序列列表["I", "dont", "know", "what", "you", "mean", "by", "glory", "Alice", "said ."] (请注意,这包括“said.”中的点。)

 str.match(/\w+/g) 

返回所有单词的列表:["I", "dont", "know", "what", "you" 、“意味着”、“通过”、 "glory", "Alice", "said"]

匹配()

 str.match(/\S+/g) 

returns a list of non-space sequences ["I", "dont", "know", "what", "you", "mean", "by", "glory", "Alice", "said."] (note that this includes the dot in "said.")

 str.match(/\w+/g) 

returns a list of all words: ["I", "dont", "know", "what", "you", "mean", "by", "glory", "Alice", "said"]

docs on match()

微凉 2025-01-15 08:11:51

在使用 split 之前应该修剪字符串。

var str = " I dont know what you mean by glory Alice said."
var trimmed = str.replace(/^\s+|\s+$/g, '');
trimmed = str.split(" ")

You should trim the string before using split.

var str = " I dont know what you mean by glory Alice said."
var trimmed = str.replace(/^\s+|\s+$/g, '');
trimmed = str.split(" ")
稚然 2025-01-15 08:11:51

我推荐 .match

str.match(/\b\w+\b/g);

这会匹配单词边界之间的单词,因此所有空格都不匹配,因此不会包含在结果数组中。

I recommend .match:

str.match(/\b\w+\b/g);

This matches words between word boundaries, so all spaces are not matched and thus not included in the resulting array.

握住你手 2025-01-15 08:11:51

我认为出现空子字符串是因为有多个空格,您可以在 for 循环中使用 Replace() 将多个空格替换为单个空格,然后使用 split() 使用单个空格拆分程序像这样:

// getting full program from div
var program = document.getElementById("ans").textContent;
//removing multiple spaces
var res = program.replace("  ", " ");
for (i = 0; i <= program.length; i++) {
  var res = res.replace("  ", " ");
}
// spliting each word using space as saperator
var result = res.split(" ");

i think empty sub-string happen because there are multiple white-spaces you can use a replace() in a for loop to replace multiple white-spaces with a single white-space then split() to split the program using a single white space like this:

// getting full program from div
var program = document.getElementById("ans").textContent;
//removing multiple spaces
var res = program.replace("  ", " ");
for (i = 0; i <= program.length; i++) {
  var res = res.replace("  ", " ");
}
// spliting each word using space as saperator
var result = res.split(" ");

夏有森光若流苏 2025-01-15 08:11:51

这就是我们所需要的:

str.trim().split(' ')

That is all that we need:

str.trim().split(' ')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文